简体   繁体   English

Python Selenium 获取页面源中的所有元素。 driver.find_elements(By.XPATH, "//*")

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

I'm trying to get all HTML tags in page_source by using the below code我正在尝试使用以下代码获取 page_source 中的所有 HTML 标签

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大多数时候它按预期工作,但对于某些后面的情况(可能在更大的 page_sources 中)它挂起并上升到异常以下

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?您对如何修复此命令或其他等效命令以获取 page_source 中的所有 HTML 元素有任何建议吗? Thank you beforehand.预先谢谢你。

Found the issue.发现了问题。 Some elements are deleted by JS after the page is fully loaded.一些元素在页面完全加载后被 JS 删除。 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.解决方案是在循环遍历每个元素时添加一个 try/except 块,因为它可能会在页面完全加载后被删除。 I hope this makes sense.我希望这是有道理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过使用 driver.find_elements(By.XPATH,'') 和 Python Selenium 来获取汽车名称 - How can i get car names by using driver.find_elements(By.XPATH,'') with Python Selenium “ find_element_by_xpath”和“ driver.find_elements(By.XPATH)”的区别是什么? - What is the difference in “find_element_by_xpath” and “driver.find_elements(By.XPATH)” Python Selenium:使用driver.find_elements获得不同的结果 - Python Selenium: Geting different results using driver.find_elements 通过href无法获取来自driver.find_elements的所有链接 - Get ALL links from driver.find_elements by href not working 迭代selenium webdriver的driver.find_elements - Iterating over selenium webdriver's driver.find_elements 蟒蛇硒-<class 'AttributeError'> 发生于 - len(driver.find_Elements(locatortype,locator)) &gt; 0 - Python Selenium-<class 'AttributeError'> occured for - len(driver.find_Elements(locatortype,locator)) > 0 driver.find_elements() 返回空列表 - driver.find_elements() is returning empty list Python:Selenium 驱动程序 find_elements_by_xpath:问题 - Python: Selenium Driver find_elements_by_xpath: Issue driver.find_elements() 没有给出预期的输出 - driver.find_elements() does not gives the expected output Selenium Python Find Elements by XPath 不返回所有预期元素 - Selenium Python Find Elements by XPath does not return all expected elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM