我正在使用python中的selenium为https://etherscan.io/编写一个刮板。在我的代码中,当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')))发布于 2022-04-10 08:05:25
@cruisepandey解决方案是可以的,但我认为我的解决方案更简单一些。而且,在我的例子中,他的解决方案重定向到错误的页面(到主页)。
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)https://stackoverflow.com/questions/71814421
复制相似问题