pycharm xpath path error
result=("//*[@]/div[2]")
SyntaxError: invalid syntax
#If you use double quotes outside, you need to use single quotes inside, and vice versa, that is
result=("//*[@id=‘ptab-0']/div[2]")
If the outside is double quotes, you also want to use double quotes inside, you need to escape with a backslash.
xpath positioning paths are inconsistent each time
xpath positioning
python+selenium locates elements whose xpath will change – use the last() function to locate the last node number
Web automation learning – the use of last() function of xpath
Problem description
When implementing functional automation, locate a certain element and directly copy its xpath. In fact, after the script runs, it is found that the element has not been located;
xpath is:
/html/body/div[6]/div[1]/div[1]/ul/li
Cause of the problem
For example, due to different operations, xpath may be:
/html/body/div[6]/div[1]/div[1]/ul/li /html/body/div[5]/div[1]/div[1]/ul/li /html/body/div[4]/div[1]/div[1]/ul/li
Solution
Through the second step, you will find that the position of a certain div in xpath changes!
However, a rule can be found that although this div is changing, it is always located at the last node;
So the last() function is cleverly introduced.
Change the writing method of xpath to:
/html/body/div[last()]/div[1]/div[1]/ul/li
After such adjustments, no matter how the operation changes, you can always be positioned correctly.
Regarding the reason why xpath is correct and still returns an empty list
1. There is a tbody in the element, and xpath cannot be parsed
2. The web page has js rendering. The source code obtained by requests is the code before rendering. Xpath cannot parse this content so it returns empty
3. The source code obtained is commented out
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.