我如何解决:
stale element reference: element is not attached to the page document
第57行,在 打印(lang.text) selenium.common.exceptions.StaleElementReferenceException:消息:陈旧元素引用:元素未附加到页面 文献 (会话信息:chrome = 63.0.3239.108) (驱动程序信息:chromedriver = 2.34.522940(1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform = Windows NT 10.0.16299 x86_64)
recommended解决方案:
try:
except StaleElementReferenceException as e:
raise e
完全跳过元素
在等待特定元素时不会删除错误。我在下面做错了什么?感谢。
脚本:
import time
from random import shuffle
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
driver = webdriver.Chrome()
driver.get('https://ubet.com/sports/soccer')
options = driver.find_elements_by_xpath('//select[./option="Soccer"]/option')
indexes = [index for index in range(len(options))]
shuffle(indexes)
for index in indexes:
wait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '(//select/optgroup/option)[%s]' % str(index + 1)))).click()
clickMe = wait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ('.large-4:nth-child(1) .lbl-offer span '))))
from selenium.webdriver.support.ui import WebDriverWait
def find(driver):
element = driver.find_elements_by_css_selector("div > div > div > div > div > div > div > div > div.row.collapse > div > div > div:nth-child(2) > div > div > div > div > div > div.row.small-collapse.medium-collapse > div:nth-child(1) > div > div > div > div.lbl-offer > span")
if element:
print(element)
return element
else:
return False
element = WebDriverWait(driver, 10).until(find)
element_text = []
for lang in element:
print(lang.text)
time.sleep(1)