【发布时间】:2020-08-30 16:11:02
【问题描述】:
我在 Python 上使用 Selenium 并尝试移动光标并单击特定元素。这适用于第一个链接,并且 HTML 的结构对于下一个链接是相同的,但是当通过相同的 web 驱动程序访问第二个链接时,我得到了第二个链接的 StaleElementReferenceException。为什么会发生这种情况,我该如何解决?下面是我正在运行的代码。非常感谢!
def getZest(url):
zestlist = []
yearlist = []
driver.get(url)
time.sleep(5)
result = False;
attempts = 0;
while(attempts < 5):
try:
Home_Value = wait.until(EC.presence_of_element_located((By.XPATH, "//a[text()='Home value']")))
action.move_to_element(Home_Value).click().perform()
zestimate = driver.find_element_by_xpath('//*[@id="ds-home-values"]/div/div[3]/button')
action.move_to_element(zestimate).perform()
result = True
break
except exceptions.StaleElementReferenceException as e:
print(e)
attempts = attempts + 1
fivenums = ["https://www.zillow.com/homedetails/212-Haddrell-St-Mount-Pleasant-SC-29464/10922911_zpid/", "https://www.zillow.com/homedetails/20-Grove-St-Hicksville-NY-11801/31127407_zpid/"]
for num in fivenums:
getZest(num)
【问题讨论】:
-
@DebanjanB 我知道您对 Selenium 有很多经验,如果您能提供帮助,那就太好了!
-
这听起来像X-Y problem。与其寻求解决问题的帮助,不如编辑您的问题并询问实际问题。你想做什么?
-
@DebanjanB 我正在尝试单击网站上的某个元素以获取同一 Web 驱动程序中的多个链接。但是,当 webdriver 移动到第二个链接时,我得到一个 StaleElementReferenceException。无论我试图访问什么链接,情况都是如此。我想摆脱这个异常并能够单击网站上的元素以获取列表中的所有链接。感谢您的帮助,我将更改问题。
标签: python selenium web-scraping staleelementreferenceexception