首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何处理StaleElementReferenceException (Selenium )?

如何处理StaleElementReferenceException (Selenium )?
EN

Stack Overflow用户
提问于 2022-04-10 06:58:30
回答 2查看 87关注 0票数 0

我正在使用python中的selenium为https://etherscan.io/编写一个刮板。在我的代码中,当StaleElementReferenceException单击一个按钮而没有找到任何解决此错误的好源时,它就会在代码中得到它。

这是密码

代码语言:javascript
复制
url = "https://etherscan.io/login?"
driver.implicitly_wait(30)
driver.get(url)
username = driver.find_element_by_xpath('//*[@id="ContentPlaceHolder1_txtUserName"]')
username.send_keys('user')
password = driver.find_element_by_xpath('//*[@id="ContentPlaceHolder1_txtPassword"]')
password.send_keys('pass')
login = driver.find_element_by_xpath('//*[@id="ContentPlaceHolder1_btnLogin"]')
login.click()
more = driver.find_element_by_xpath('//*[@id="moreMegaMenu"]')
more.click()
word_cloud = driver.find_element_by_xpath('//*[@id="LI41"]/a').click()
soup = BeautifulSoup(driver.page_source, 'html.parser')
wait = WebDriverWait(driver, 10)
dropdown = driver.find_elements_by_css_selector('.dropdown-toggle')
for ele in dropdown:
    ele.click()
    time.sleep(1)
    account = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'.d-block')))
    driver.execute_script("arguments[0].click();", account)
    time.sleep(1)
    driver.execute_script("window.history.go(-1)")

错误信息:

代码语言:javascript
复制
StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=100.0.4896.75)
代码语言:javascript
复制
StaleElementReferenceException            Traceback (most recent call last)
<ipython-input-108-2804d338c51f> in <module>
      1 dropdown = driver.find_elements_by_css_selector('.dropdown-toggle')
      2 for ele in dropdown:
----> 3     ele.click() # this is the line where it is showing error
      4     time.sleep(1)
      5     account = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'.d-block')))
EN

Stack Overflow用户

发布于 2022-04-10 08:05:25

@cruisepandey解决方案是可以的,但我认为我的解决方案更简单一些。而且,在我的例子中,他的解决方案重定向到错误的页面(到主页)。

代码语言:javascript
复制
url = "https://etherscan.io/login?"
driver.implicitly_wait(30)
driver.get(url)
username = driver.find_element_by_xpath('//*[@id="ContentPlaceHolder1_txtUserName"]')
username.send_keys('user')
password = driver.find_element_by_xpath('//*[@id="ContentPlaceHolder1_txtPassword"]')
password.send_keys('pass')
login = driver.find_element_by_xpath('//*[@id="ContentPlaceHolder1_btnLogin"]')
login.click()
more = driver.find_element_by_xpath('//*[@id="moreMegaMenu"]')
more.click()
word_cloud = driver.find_element_by_xpath('//*[@id="LI41"]/a').click()
soup = BeautifulSoup(driver.page_source, 'html.parser')
wait = WebDriverWait(driver, 10)

num_elements = len(driver.find_elements_by_css_selector('.dropdown-toggle'))

for i in range(num_elements):
    ele = driver.find_elements_by_css_selector('.dropdown-toggle')[i]
    ele.click()
    time.sleep(1)
    account = wait.until(EC.element_to_be_clickable((By.XPATH, f'//*[@id="content"]/div[3]/div/div/div[3]/div[{i+1}]/div/div/a[1]')))
    driver.execute_script("arguments[0].click();", account)
    time.sleep(1)
    driver.execute_script("window.history.go(-1)")
    time.sleep(1)
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71814421

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档