如何处理StaleElementReferenceException(SeleniumPython)



我正在为https://etherscan.io/在python中使用硒。当它点击一个按钮时,我的代码中出现了StaleElementReferenceException,但没有找到任何好的源代码来解决这个错误。

这是代码

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)")

错误消息:

StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=100.0.4896.75)
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')))

@cruisepandy解决方案还可以,但我认为我的解决方案稍微简单一点。此外,在我的情况下,他的解决方案重定向到错误的页面(主页(。

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)

问题解释:

当你点击如下账户时:

driver.execute_script("arguments[0].click();", account)

这会将您重定向到same tab and windows中的new page

但是,当您返回主页时使用:

driver.execute_script("window.history.go(-1)")

现在,这些元素在自然界中已经过时了。

此外,您不应该真正点击more按钮,而是使用ActionChain将鼠标悬停在它上面。

这条线

account = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'.d-block')))

第二个和后续元素不起作用,因为.d-block不是所有其他下拉列表的正确定位符。相反,使用相对xpath

.//following-sibling::div/a

解决方案:

继续在循环中定义web元素。

代码:

wait = WebDriverWait(driver, 20)
url = "https://etherscan.io/login?"
driver.maximize_window()
#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()
try:
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#btnCookie"))).click()
print('clicked on cookies button successfully.')
except:
print('could not clicked on cookies button.')
pass
more = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='moreMegaMenu']")))
ActionChains(driver).move_to_element(more).pause(2).perform()
word_cloud = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='LI41']/a")))
word_cloud.click()
soup = BeautifulSoup(driver.page_source, 'html.parser')
dropdown = driver.find_elements_by_css_selector('.dropdown-toggle')
i = 1
for ele in dropdown:
ele = wait.until(EC.presence_of_element_located((By.XPATH, f"(//button[contains(@class,'dropdown-toggle')])[{i}]")))
driver.execute_script("arguments[0].scrollIntoView(true);", ele)
ele.click()
time.sleep(1)
account = wait.until(EC.element_to_be_clickable((By.XPATH, ".//following-sibling::div/a")))
driver.execute_script("arguments[0].click();", account)
time.sleep(1)
driver.execute_script("window.history.go(-1)")
i = i + 1

输出:

clicked on cookies button successfully.
ETHER PRICE $3,244.85 @ 0.07608 BTC (+1.09%)
ETHER PRICE $3,244.85 @ 0.07608 BTC (+1.09%)
ETHER PRICE $3,244.85 @ 0.07608 BTC (+1.09%)
ETHER PRICE $3,244.85 @ 0.07608 BTC (+1.09%)
ETHER PRICE $3,244.85 @ 0.07608 BTC (+1.09%)
ETHER PRICE $3,244.85 @ 0.07608 BTC (+1.09%)
ETHER PRICE $3,244.85 @ 0.07608 BTC (+1.09%)
ETHER PRICE $3,244.85 @ 0.07608 BTC (+1.09%)
ETHER PRICE $3,244.71 @ 0.07609 BTC (+1.08%)
ETHER PRICE $3,244.71 @ 0.07609 BTC (+1.08%)
ETHER PRICE $3,244.71 @ 0.07609 BTC (+1.08%)
ETHER PRICE $3,244.71 @ 0.07609 BTC (+1.08%)
ETHER PRICE $3,244.71 @ 0.07609 BTC (+1.08%)
ETHER PRICE $3,244.71 @ 0.07609 BTC (+1.08%)
ETHER PRICE $3,244.71 @ 0.07609 BTC (+1.08%)
ETHER PRICE $3,244.71 @ 0.07609 BTC (+1.08%)
ETHER PRICE $3,244.71 @ 0.07609 BTC (+1.08%)
ETHER PRICE $3,244.71 @ 0.07609 BTC (+1.08%)
ETHER PRICE $3,244.71 @ 0.07609 BTC (+1.08%)
ETHER PRICE $3,244.71 @ 0.07609 BTC (+1.08%)
ETHER PRICE $3,244.53 @ 0.07608 BTC (+1.08%)
ETHER PRICE $3,244.53 @ 0.07608 BTC (+1.08%)
ETHER PRICE $3,244.53 @ 0.07608 BTC (+1.08%)
ETHER PRICE $3,244.53 @ 0.07608 BTC (+1.08%)
ETHER PRICE $3,244.53 @ 0.07608 BTC (+1.08%)
ETHER PRICE $3,244.53 @ 0.07608 BTC (+1.08%)
ETHER PRICE $3,244.53 @ 0.07608 BTC (+1.08%)
ETHER PRICE $3,244.53 @ 0.07608 BTC (+1.08%)
ETHER PRICE $3,244.53 @ 0.07608 BTC (+1.08%)
ETHER PRICE $3,244.53 @ 0.07608 BTC (+1.08%)
ETHER PRICE $3,244.53 @ 0.07608 BTC (+1.08%)
ETHER PRICE $3,244.53 @ 0.07608 BTC (+1.08%)
ETHER PRICE $3,244.53 @ 0.07608 BTC (+1.08%)
ETHER PRICE $3,244.39 @ 0.07608 BTC (+1.07%)
ETHER PRICE $3,244.39 @ 0.07608 BTC (+1.07%)
ETHER PRICE $3,244.39 @ 0.07608 BTC (+1.07%)
ETHER PRICE $3,244.39 @ 0.07608 BTC (+1.07%)
ETHER PRICE $3,244.39 @ 0.07608 BTC (+1.07%)
ETHER PRICE $3,244.39 @ 0.07608 BTC (+1.07%)
ETHER PRICE $3,244.39 @ 0.07608 BTC (+1.07%)
ETHER PRICE $3,244.39 @ 0.07608 BTC (+1.07%)
ETHER PRICE $3,244.39 @ 0.07608 BTC (+1.07%)
ETHER PRICE $3,244.39 @ 0.07608 BTC (+1.07%)
ETHER PRICE $3,244.39 @ 0.07608 BTC (+1.07%)
ETHER PRICE $3,244.39 @ 0.07608 BTC (+1.07%)
ETHER PRICE $3,244.39 @ 0.07608 BTC (+1.07%)
ETHER PRICE $3,243.26 @ 0.07607 BTC (+1.06%)
ETHER PRICE $3,243.26 @ 0.07607 BTC (+1.06%)
ETHER PRICE $3,243.26 @ 0.07607 BTC (+1.06%)
ETHER PRICE $3,243.26 @ 0.07607 BTC (+1.06%)
ETHER PRICE $3,243.26 @ 0.07607 BTC (+1.06%)
ETHER PRICE $3,243.26 @ 0.07607 BTC (+1.06%)
ETHER PRICE $3,243.26 @ 0.07607 BTC (+1.06%)
ETHER PRICE $3,243.26 @ 0.07607 BTC (+1.06%)
ETHER PRICE $3,243.26 @ 0.07607 BTC (+1.06%)
ETHER PRICE $3,243.26 @ 0.07607 BTC (+1.06%)
ETHER PRICE $3,243.26 @ 0.07607 BTC (+1.06%)
ETHER PRICE $3,243.26 @ 0.07607 BTC (+1.06%)
ETHER PRICE $3,243.26 @ 0.07607 BTC (+1.06%)
ETHER PRICE $3,243.26 @ 0.07607 BTC (+1.06%)
ETHER PRICE $3,243.26 @ 0.07607 BTC (+1.06%)
ETHER PRICE $3,243.26 @ 0.07607 BTC (+1.06%)
ETHER PRICE $3,243.26 @ 0.07607 BTC (+1.06%)
ETHER PRICE $3,243.26 @ 0.07607 BTC (+1.06%)
ETHER PRICE $3,243.26 @ 0.07607 BTC (+1.06%)
ETHER PRICE $3,243.26 @ 0.07607 BTC (+1.06%)
ETHER PRICE $3,243.26 @ 0.07607 BTC (+1.06%)
ETHER PRICE $3,243.26 @ 0.07607 BTC (+1.06%)
ETHER PRICE $3,243.26 @ 0.07607 BTC (+1.06%)
ETHER PRICE $3,242.79 @ 0.07606 BTC (+1.04%)
ETHER PRICE $3,242.79 @ 0.07606 BTC (+1.04%)
ETHER PRICE $3,242.79 @ 0.07606 BTC (+1.04%)
ETHER PRICE $3,242.79 @ 0.07606 BTC (+1.04%)
ETHER PRICE $3,242.79 @ 0.07606 BTC (+1.04%)
ETHER PRICE $3,242.79 @ 0.07606 BTC (+1.04%)
ETHER PRICE $3,242.79 @ 0.07606 BTC (+1.04%)
ETHER PRICE $3,242.79 @ 0.07606 BTC (+1.04%)
ETHER PRICE $3,242.79 @ 0.07606 BTC (+1.04%)
ETHER PRICE $3,242.79 @ 0.07606 BTC (+1.04%)
ETHER PRICE $3,242.79 @ 0.07606 BTC (+1.04%)
ETHER PRICE $3,242.50 @ 0.07606 BTC (+1.03%)
ETHER PRICE $3,242.50 @ 0.07606 BTC (+1.03%)
ETHER PRICE $3,242.50 @ 0.07606 BTC (+1.03%)
ETHER PRICE $3,242.50 @ 0.07606 BTC (+1.03%)
ETHER PRICE $3,242.50 @ 0.07606 BTC (+1.03%)
ETHER PRICE $3,242.50 @ 0.07606 BTC (+1.03%)
ETHER PRICE $3,242.50 @ 0.07606 BTC (+1.03%)
ETHER PRICE $3,242.50 @ 0.07606 BTC (+1.03%)
ETHER PRICE $3,242.50 @ 0.07606 BTC (+1.03%)
ETHER PRICE $3,242.50 @ 0.07606 BTC (+1.03%)
ETHER PRICE $3,242.50 @ 0.07606 BTC (+1.03%)
ETHER PRICE $3,242.14 @ 0.07607 BTC (+1.02%)
ETHER PRICE $3,242.14 @ 0.07607 BTC (+1.02%)
ETHER PRICE $3,242.14 @ 0.07607 BTC (+1.02%)
ETHER PRICE $3,242.14 @ 0.07607 BTC (+1.02%)
ETHER PRICE $3,242.14 @ 0.07607 BTC (+1.02%)
ETHER PRICE $3,242.14 @ 0.07607 BTC (+1.02%)
ETHER PRICE $3,242.14 @ 0.07607 BTC (+1.02%)
ETHER PRICE $3,242.14 @ 0.07607 BTC (+1.02%)
ETHER PRICE $3,242.14 @ 0.07607 BTC (+1.02%)
ETHER PRICE $3,242.14 @ 0.07607 BTC (+1.02%)
ETHER PRICE $3,242.14 @ 0.07607 BTC (+1.02%)
ETHER PRICE $3,242.14 @ 0.07607 BTC (+1.02%)
ETHER PRICE $3,242.14 @ 0.07607 BTC (+1.02%)
ETHER PRICE $3,241.93 @ 0.07607 BTC (+1.01%)
ETHER PRICE $3,241.93 @ 0.07607 BTC (+1.01%)
ETHER PRICE $3,241.93 @ 0.07607 BTC (+1.01%)
ETHER PRICE $3,241.93 @ 0.07607 BTC (+1.01%)
ETHER PRICE $3,241.93 @ 0.07607 BTC (+1.01%)
ETHER PRICE $3,241.93 @ 0.07607 BTC (+1.01%)
ETHER PRICE $3,241.93 @ 0.07607 BTC (+1.01%)
ETHER PRICE $3,241.93 @ 0.07607 BTC (+1.01%)

等等。

相关内容

  • 没有找到相关文章

最新更新