Python Selenium get all elements in page source. driver.find_elements(By.XPATH, "//*")

326 Views Asked by At

I'm trying to get all HTML tags in page_source by using the below code

driver.find_elements(By.XPATH, "//*")

Most of the time it works as expected but for some rear cases (probably in bigger page_sources) it hangs and rises below the exception

Message: stale element reference: element is not attached to the page document

Do you have any suggestions on how to fix this or another equivalent command to get all HTML elements in page_source? Thank you beforehand.

1

There are 1 best solutions below

0
On

Found the issue. Some elements are deleted by JS after the page is fully loaded. the solution is to add a try/except block whenever looping through each element as it might be deleted after the page is fully loaded. I hope this makes sense.