我试图使用python selenium刮掉Stackoverflow。
当我尝试打印每个问题的链接(href)时,我得到StaleElementReferenceException
几个元素。
以下是例外情况
selenium.common.exceptions.StaleElementReferenceException: Message: The element reference is stale. Either the element is no longer attached to the DOM or the page has been refreshed.
我已经尝试过两次
WebDriverWait(driver, 3).until(EC.presence_of_element_located(
(By.XPATH, xpath)))
和
driver.implicitly_wait(30)
我已经检查了几个问题,但无法解决这个问题 StaleElementReferenceException: Element is no longer attached to the DOM: Selenium
代码段
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
driver.get("https://stackoverflow.com/questions")
try:
WebDriverWait(driver, 3).until(EC.presence_of_element_located(
(By.XPATH, '//div[@id="tabs"]/a[@class="youarehere"]')))
print "Page is ready!"
except TimeoutException:
print "Time exceeded"
noofquestionsperpage = driver.find_element_by_xpath(
'//div[@class="page-sizer fr"]/a[@class="page-numbers current"]').text
requirednumberofposts = 30
numberofpagesclick = requirednumberofposts / int(noofquestionsperpage)
print numberofpagesclick
if numberofpagesclick > 1:
for i in range(numberofpagesclick):
for a in driver.find_elements_by_xpath('//*[@id="questions"]/div[@class="question-summary"]'):
try:
WebDriverWait(driver, 3).until(EC.presence_of_element_located(
(By.XPATH, './/div[@class="summary"]/h3/a')))
print a.find_element_by_xpath('.//div[@class="summary"]/h3/a').get_attribute('href')
except Exception, e:
print str(e).strip()
try:
driver.find_element_by_xpath(
'//div[@class="pager fl"]/a[6]/span[@class="page-numbers next"]').click()
except:
print "All questons loaded"
更新
找到另一种方法来迭代stackoverflow中的问题,发布代码。现在我根据每个问题的div id生成xpath,而不是使用导致questions
的{{1}} webelement早。
StaleElementReferenceException