1

Here is what I currently do to get the first element of a page (or parent element):

first_element = page_or_parent.find_element_by_xpath('//*')

The problem I've only just run into recently (Chrome and chromedriver version 84) is that sometimes this throws an exception:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*"}

Calling the same method again usually retrieves the element so I presume the document tree is temporarily unavailable. However, the web driver log doesn't indicate anything strange:

[1597164661.317][INFO]: [d88526be26bd6364d1b0ed2dab9d5733] COMMAND FindElement {
"using": "xpath",
"value": "//*"
}
[1597164661.317][INFO]: Waiting for pending navigations...
[1597164661.317][INFO]: Done waiting for pending navigations. Status: ok
[1597164661.418][INFO]: Waiting for pending navigations...
[1597164661.418][INFO]: Done waiting for pending navigations. Status: ok
[1597164661.418][INFO]: [d88526be26bd6364d1b0ed2dab9d5733] RESPONSE FindElement ERROR no such element: Unable to locate element: {"method":"xpath","selector":"//*"}
(Session info: chrome=84.0.4147.89)

My main question is then how to get the first element reliably?

Is there a better way than using an XPath as I am doing? I thought about find_elements_by_xpath() (plural) but if there's a lot of elements then that's likely to generate a lot of data and run much slower. The only solution I can think of currently is to catch the exception if it occurs, wait/sleep, and then try again but that feels very dirty.

1 Answer 1

2

You have to wait for some time so that the page can load fully and then you can resume your script. or else you can also use the wait as shown below -

# Import 
from selenium.webdriver.support import expected_conditions

wait = WebDriverWait(driver, 5)

wait.until(expected_conditions.presence_of_element_located((By.XPATH, "Your XPATH")))
Sign up to request clarification or add additional context in comments.

3 Comments

The page is fully loaded when I encounter the problem; I confirmed that the document ready state is "complete"
Have you tried using the expected_condition mentioned in the above resolution? If yes then what error you are getting? Same "NoSuchElementException"?
It would work -- it's the same as sleeping which I've already verified. I think this is a Chrome issue on Linux as it's only come up recently for code that has otherwise run non-stop for 2 years. I'm not going to add sleeps into a helper function for getting the first element; so for now I'm just dealing with the exception

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.