【发布时间】:2019-05-03 16:23:47
【问题描述】:
我正在尝试浏览目录中的每个产品并打印产品图片链接。以下是我的代码。
product_links = driver.find_elements_by_css_selector(".product-link")
for link in product_links:
driver.get(link.get_attribute("href"))
images = driver.find_elements_by_css_selector("#gallery img")
for image in images:
print(image.get_attribute("src"))
driver.back()
但我收到错误selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document,我认为这是因为当我们返回目录页面时,页面再次加载,product_links 中的元素引用变得陈旧。
我们如何避免这个问题?有没有更好的解决方案?
【问题讨论】:
-
最简单的解决方法是refresh页面重新建立dom对象
-
@G.Anderson - 还是同样的问题
标签: python selenium-webdriver automated-tests