过时的元素参考:元素未附加到具有 chrome 网络驱动程序的页面文档
stale element reference: element is not attached to the page document with the chrome web driver
下午我已经阅读了很多处理这个问题的线程,但不幸的是我目前 运行 缺少解决方案:(
我试着抓取这个网站:https://www.kumon.co.uk/find-a-tutor/
我使用此代码来存储每个 url 不同商店。为此,我必须迭代下一页直到最后一页。
这是我使用的代码:
def get_urls(url) -> list:
# Get all URLs to the store pages
options = Options()
# options.add_argument('--headless')
path_chromedriver = Path(__file__).parent.parent.joinpath('externals/chromedriver')
browser = webdriver.Chrome(str(path_chromedriver), chrome_options=options)
browser.get(url)
inputElement = browser.find_element_by_id("centre_search")
inputElement.send_keys('london')
inputElement.send_keys(Keys.ENTER)
store_url = []
links = browser.find_elements_by_link_text('Choose Centre')
for link in links:
href = link.get_attribute('href')
store_url.append(href)
while browser.find_element_by_xpath("//ul[@class='pagination']//li[last()]/a/small"):
WebDriverWait(browser, 20).until(
EC.element_to_be_clickable((By.XPATH, "//ul[@class='pagination']//li[last()]/a/small"))).click()
links = browser.find_elements_by_link_text('Choose Centre')
for link in links:
href = link.get_attribute('href')
store_url.append(href)
return store_url
不幸的是我得到了
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
使用 Try...Except 不是一个好的解决方案,我正在寻找一个可靠的解决方案。
我应该从 Chrome 切换到 Firefox 吗?
提前致谢,
尼古拉斯.
不确定为什么您认为 try
/except
不是好的解决方案,但这正是您需要的:
from selenium.common.exceptions import WebDriverException
def get_urls(url) -> list:
# Get all URLs to the store pages
options = Options()
# options.add_argument('--headless')
path_chromedriver = Path(__file__).parent.parent.joinpath('externals/chromedriver')
browser = webdriver.Chrome(str(path_chromedriver), chrome_options=options)
browser.get(url)
inputElement = browser.find_element_by_id("centre_search")
inputElement.send_keys('london')
inputElement.send_keys(Keys.ENTER)
links = browser.find_elements_by_link_text('Choose Centre')
store_url = [link.get_attribute("href") for link in links]
while True:
try:
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//li[last()][not(normalize-space(@class))]/a[@data-page]"))).click()
WebDriverWait(browser, 10).until(EC.staleness_of(links[-1]))
except WebDriverException:
break
links = WebDriverWait(browser, 10).until(EC.visibility_of_all_elements_located((By.LINK_TEXT, 'Choose Centre')))
store_url.extend([link.get_attribute("href") for link in links])
return store_url
下午我已经阅读了很多处理这个问题的线程,但不幸的是我目前 运行 缺少解决方案:(
我试着抓取这个网站:https://www.kumon.co.uk/find-a-tutor/
我使用此代码来存储每个 url 不同商店。为此,我必须迭代下一页直到最后一页。
这是我使用的代码:
def get_urls(url) -> list:
# Get all URLs to the store pages
options = Options()
# options.add_argument('--headless')
path_chromedriver = Path(__file__).parent.parent.joinpath('externals/chromedriver')
browser = webdriver.Chrome(str(path_chromedriver), chrome_options=options)
browser.get(url)
inputElement = browser.find_element_by_id("centre_search")
inputElement.send_keys('london')
inputElement.send_keys(Keys.ENTER)
store_url = []
links = browser.find_elements_by_link_text('Choose Centre')
for link in links:
href = link.get_attribute('href')
store_url.append(href)
while browser.find_element_by_xpath("//ul[@class='pagination']//li[last()]/a/small"):
WebDriverWait(browser, 20).until(
EC.element_to_be_clickable((By.XPATH, "//ul[@class='pagination']//li[last()]/a/small"))).click()
links = browser.find_elements_by_link_text('Choose Centre')
for link in links:
href = link.get_attribute('href')
store_url.append(href)
return store_url
不幸的是我得到了
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
使用 Try...Except 不是一个好的解决方案,我正在寻找一个可靠的解决方案。 我应该从 Chrome 切换到 Firefox 吗?
提前致谢, 尼古拉斯.
不确定为什么您认为 try
/except
不是好的解决方案,但这正是您需要的:
from selenium.common.exceptions import WebDriverException
def get_urls(url) -> list:
# Get all URLs to the store pages
options = Options()
# options.add_argument('--headless')
path_chromedriver = Path(__file__).parent.parent.joinpath('externals/chromedriver')
browser = webdriver.Chrome(str(path_chromedriver), chrome_options=options)
browser.get(url)
inputElement = browser.find_element_by_id("centre_search")
inputElement.send_keys('london')
inputElement.send_keys(Keys.ENTER)
links = browser.find_elements_by_link_text('Choose Centre')
store_url = [link.get_attribute("href") for link in links]
while True:
try:
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//li[last()][not(normalize-space(@class))]/a[@data-page]"))).click()
WebDriverWait(browser, 10).until(EC.staleness_of(links[-1]))
except WebDriverException:
break
links = WebDriverWait(browser, 10).until(EC.visibility_of_all_elements_located((By.LINK_TEXT, 'Choose Centre')))
store_url.extend([link.get_attribute("href") for link in links])
return store_url