Selenium EC.element_to_be_clickable 仍然 returns 陈旧元素错误
Selenium EC.element_to_be_clickable still returns stale element error
我不确定这有什么问题。我使用 EC.element_to_be_clickable()
对吗?我收到错误消息:“selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
”。我很确定 XPATH 是有效的,甚至尝试过指定相同元素的另一个 XPATH。
我的代码:
driver.get("https://browzine.com/libraries/1374/subjects")
parent_url = "https://browzine.com/libraries/1374/subjects"
wait = WebDriverWait(driver, 10)
subjects_avail = driver.find_elements(By.XPATH, "//span[@class='subjects-list-subject-name']")
subjects = 0
for sub in subjects_avail:
WebDriverWait(driver, 5).until(EC.element_to_be_clickable(
(By.XPATH, "//span[@class='subjects-list-subject-name']")))
ActionChains(driver).move_to_element(sub).click(sub).perform()
subjects = +1
driver.get(parent_url)
每次单击 sub
元素时,driver 都会导航到新页面。
然后你用
再次打开主页
driver.get(parent_url)
但是 subjects_avail
列表中的所有 Web 元素都变得陈旧,因为 driver 已经离开了原始主页。
为了使您的代码正常工作,您必须在每次返回主页时再次获取 subjects_avail
列表,然后 select 正确的子标题元素。
像这样:
url = "https://browzine.com/libraries/1374/subjects"
subj_list_xpath = "//span[@class='subjects-list-subject-name']"
driver.get(url)
wait = WebDriverWait(driver, 10)
subjects_avail = driver.find_elements(By.XPATH, subj_list_xpath)
for i in range(len(subjects_avail)):
WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, subj_list_xpath)))
subjects_avail = driver.find_elements(By.XPATH, subj_list_xpath)
ActionChains(driver).move_to_element(subjects_avail[i]).click(subjects_avail[i]).perform()
driver.get(url)
我不确定这有什么问题。我使用 EC.element_to_be_clickable()
对吗?我收到错误消息:“selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
”。我很确定 XPATH 是有效的,甚至尝试过指定相同元素的另一个 XPATH。
我的代码:
driver.get("https://browzine.com/libraries/1374/subjects")
parent_url = "https://browzine.com/libraries/1374/subjects"
wait = WebDriverWait(driver, 10)
subjects_avail = driver.find_elements(By.XPATH, "//span[@class='subjects-list-subject-name']")
subjects = 0
for sub in subjects_avail:
WebDriverWait(driver, 5).until(EC.element_to_be_clickable(
(By.XPATH, "//span[@class='subjects-list-subject-name']")))
ActionChains(driver).move_to_element(sub).click(sub).perform()
subjects = +1
driver.get(parent_url)
每次单击 sub
元素时,driver 都会导航到新页面。
然后你用
driver.get(parent_url)
但是 subjects_avail
列表中的所有 Web 元素都变得陈旧,因为 driver 已经离开了原始主页。
为了使您的代码正常工作,您必须在每次返回主页时再次获取 subjects_avail
列表,然后 select 正确的子标题元素。
像这样:
url = "https://browzine.com/libraries/1374/subjects"
subj_list_xpath = "//span[@class='subjects-list-subject-name']"
driver.get(url)
wait = WebDriverWait(driver, 10)
subjects_avail = driver.find_elements(By.XPATH, subj_list_xpath)
for i in range(len(subjects_avail)):
WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, subj_list_xpath)))
subjects_avail = driver.find_elements(By.XPATH, subj_list_xpath)
ActionChains(driver).move_to_element(subjects_avail[i]).click(subjects_avail[i]).perform()
driver.get(url)