如何解决StaleElementReferenceException:Message:stale element reference:element未附加错误(使用Python的Selenium Ubuntu EC2)
我正在尝试提取此页面中“Nb B”列的所有数据: 当我在Ubuntu PC上运行我的代码时,它工作得很好,但当我尝试使用EC2 Ubuntu时,它并没有给我好的回报 这是我的服务器:ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-20200611(ami-0a63f96e85105c6d3) 以下是python脚本:如何解决StaleElementReferenceException:Message:stale element reference:element未附加错误(使用Python的Selenium Ubuntu EC2),python,selenium-webdriver,xpath,css-selectors,webdriverwait,Python,Selenium Webdriver,Xpath,Css Selectors,Webdriverwait,我正在尝试提取此页面中“Nb B”列的所有数据: 当我在Ubuntu PC上运行我的代码时,它工作得很好,但当我尝试使用EC2 Ubuntu时,它并没有给我好的回报 这是我的服务器:ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-20200611(ami-0a63f96e85105c6d3) 以下是python脚本: #!/usr/bin/python3 # -*- coding: utf-8 -*- from selenium
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
options.add_argument("window-size=1400,800")
options.add_argument("--no-sandbox")
options.add_argument("--disable-gpu")
driver = webdriver.Chrome(options=options)
driver.get('https://www.coteur.com/cotes-foot.php')
#Store url associated with the soccer games
url_links = []
for i in driver.find_elements_by_xpath('//a[contains(@href, "match/cotes-")]'):
url_links.append(i.get_attribute('href'))
print(len(url_links), '\n')
nb_bookies = []
for i in driver.find_elements_by_xpath('//tr[@id and @role="row" ]/td[last()]'):
nb_bookies.append(i.text)
print(nb_bookies)
以下是输出:
Traceback (most recent call last):
File "./coteurchrome.py", line 25, in <module>
url_links.append(i.get_attribute('href'))
File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 141, in get_attribute
self, name)
File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 636, in execute_script
'args': converted_args})['value']
File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: headless chrome=83.0.4103.116)
回溯(最近一次呼叫最后一次):
文件“/coteurchrome.py”,第25行,在
url\u links.append(i.get\u属性('href'))
文件“/home/ubuntu/.local/lib/python3.6/site packages/selenium/webdriver/remote/webelement.py”,第141行,在get_属性中
(姓名)
文件“/home/ubuntu/.local/lib/python3.6/site packages/selenium/webdriver/remote/webdriver.py”,第636行,在execute_脚本中
'args':已转换的_args})['value']
文件“/home/ubuntu/.local/lib/python3.6/site packages/selenium/webdriver/remote/webdriver.py”,第321行,在execute中
self.error\u handler.check\u响应(响应)
文件“/home/ubuntu/.local/lib/python3.6/site packages/selenium/webdriver/remote/errorhandler.py”,第242行,在check_响应中
引发异常类(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.StaleElementReferenceException:消息:stale元素引用:元素未附加到页面文档
(会话信息:无头镀铬=83.0.4103.116)
要打印href属性的值,您必须为位于()的所有元素的可见性导出href属性,您可以使用以下任一选项:
- 使用
XPATH
:
driver.get("https://www.coteur.com/cotes-foot.php")
print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//a[contains(@href, 'match/cotes-')]")))])
- 使用
CSS\u选择器
:
driver.get("https://www.coteur.com/cotes-foot.php")
print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "a[href*='match/cotes-']")))])
- 控制台输出:
['https://www.coteur.com/match/cotes-millwall-blackburn-rid1109557.html', 'https://www.coteur.com/match/cotes-cardiff-derby-county-rid1109558.html', 'https://www.coteur.com/match/cotes-sheffield-w-huddersfield-rid1109559.html', 'https://www.coteur.com/match/cotes-luton-queens-park-rangers-rid1109563.html', 'https://www.coteur.com/match/cotes-chelsea-norwich-rid1109746.html', 'https://www.coteur.com/match/cotes-atalanta-brescia-rid1109694.html', 'https://www.coteur.com/match/cotes-gil-vicente-tondela-rid1109683.html', 'https://www.coteur.com/match/cotes-benfica-vitoria-guimaraes-rid1109899.html', 'https://www.coteur.com/match/cotes-orlando-city-new-york-city-fc-rid1109292.html', 'https://www.coteur.com/match/cotes-philadelphia-union-inter-miami-cf-rid1109809.html', 'https://www.coteur.com/match/cotes-ural-ekaterinburg-arsenal-tula-rid1109831.html', 'https://www.coteur.com/match/cotes-spartak-moscou-terek-akhmat-grozni-rid1109833.html', 'https://www.coteur.com/match/cotes-krylya-sovetov-fc-krasnodar-rid1109834.html', 'https://www.coteur.com/match/cotes-moreirense-pacos-ferreira-rid1109682.html', 'https://www.coteur.com/match/cotes-brentford-preston-rid1109561.html', 'https://www.coteur.com/match/cotes-bristol-c-stoke-rid1109568.html', 'https://www.coteur.com/match/cotes-fk-bodo-glimt-kristiansund-bk-rid1109735.html', 'https://www.coteur.com/match/cotes-molde-viking-rid1109736.html', 'https://www.coteur.com/match/cotes-sandefjord-aalesund-rid1109737.html', 'https://www.coteur.com/match/cotes-stromsgodset-mjondalen-rid1109738.html', 'https://www.coteur.com/match/cotes-valerenga-fk-haugesund-rid1109740.html', 'https://www.coteur.com/match/cotes-shakhtar-donetsk-kolos-kovalivka-rid1109948.html']
- 注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
对于Nb B列,我尝试了以下代码行:
print([my_elem for my_elem in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//tr[@id and @role="row" ]/td[last()]")))])
但是我有一个语法错误谢谢你的反馈,但是我可以用print(url_链接)打印url。在我的例子中,我想提取列“Nb B”的数据,url页面表格的最后一列:@ahmedaao观察错误stacktrace,你的错误是Traceback(最近一次调用):File./coteurchrome.py),第25行,在url_links.append(i.get_属性('href'))
中,这意味着未收集href
s。因此我的答案是,我对Nb B列尝试了相同的方法,但又出现了一个错误。这是我尝试过的一行:print([WebDriverWait(driver,10)中我的元素的我的元素)。直到(EC.visibility\u所有元素的位置((By.XPATH,“//tr[@id和@role=“row”]/td[last()”))