通过 xpath 枚举元素时的 Selenium chromedriver 异常 [英] Selenium chromedriver exception when enumerating elements by xpath
问题描述
我正在尝试将 Selenium 脚本从 Firefox 转换为 Chrome.该代码在 x86_64 上的 Firefox 和 geckodriver 上按预期工作.geckodriver 不能很好地支持 ARM,所以我正在尝试迁移到 Chrome.
I'm trying to convert a Selenium script from Firefox to Chrome. The code works as expected with Firefox and geckodriver on x86_64. geckodriver does not support ARM well so I am attempting to move to Chrome.
Chromium 和 chromedriver 在使用 driver.find_elements_by_xpath('//*[@id]')
时导致异常.例外是 selenium.common.exceptions.WebDriverException: Message: chrome notreachable
.
Chromium and chromedriver is causing an exception when using driver.find_elements_by_xpath('//*[@id]')
. The exception is selenium.common.exceptions.WebDriverException: Message: chrome not reachable
.
有什么问题,我该如何解决?
What is the problem and how do I fix it?
这是测试程序.
$ cat test.py
#!/usr/bin/env python3
import sys
import selenium
from packaging import version
from selenium import webdriver
#from selenium.webdriver.firefox.options import Options
from selenium.webdriver.chrome.options import Options
def main():
#################################################
if version.parse(selenium.__version__) >= version.parse("3.0"):
opts = Options()
opts.headless = True
opts.binary_location = "/usr/bin/chromium"
#driver = webdriver.Firefox(options=opts)
driver = webdriver.Chrome(chrome_options=opts)
driver.maximize_window()
else:
#profile = webdriver.FirefoxProfile()
profile = webdriver.ChromeProfile()
profile.headless = True
profile.binary_location = "/usr/bin/chromium"
#driver = webdriver.Firefox(profile)
driver = webdriver.Chrome(profile)
driver.maximize_window()
agent = driver.execute_script('return navigator.userAgent')
print(agent)
#################################################
driver.get("https://complaints.donotcall.gov/complaint/complaintcheck.aspx")
driver.implicitly_wait(3)
ids = driver.find_elements_by_xpath('//*[@id]')
for ii in ids:
print(ii.get_attribute('id'))
#################################################
driver.quit()
if __name__ == "__main__":
main()
<小时>
这是尝试使用 Chrome 和 chromedriver 枚举 Xpath 时的例外情况.
Here is the exception when attempting to enumerate the Xpaths using Chrome and chromedriver.
$ ./test.py
Mozilla/5.0 (X11; Linux armv7l) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/72.0.3626.122 Safari/537.36
Traceback (most recent call last):
File "./test.py", line 50, in <module>
main()
File "./test.py", line 41, in main
ids = driver.find_elements_by_xpath('//*[@id]')
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 410, in find_elements_by_xpath
return self.find_elements(by=By.XPATH, value=xpath)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 1007, in find_elements
'value': value})['value'] or []
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: chrome not reachable
(Session info: headless chrome=72.0.3626.122)
(Driver info: chromedriver=72.0.3626.122,platform=Linux 4.4.132+ armv7l)
<小时>
此代码:
This code:
ids = driver.find_elements_by_xpath('//*[@id]')
for val in ids:
print(val.get_attribute('id'))
应该返回如下内容:
Head1
_fed_an_ua_tag
bdyComplaint
top
changeLang
topnav
navbtn
mobileChangeLang
Form1
__EVENTTARGET
__EVENTARGUMENT
__VIEWSTATE
__VIEWSTATEGENERATOR
__EVENTVALIDATION
StepOnePanel
StepOneEntryPanel
ErrorMsg
PhoneTextBox
DateOfCallTextBox
TimeOfCallDropDownList
ddlMinutes
PrerecordMessageYESRadioButton
PrerecordMessageNORadioButton
PhoneCallRadioButton
MobileTextMessageRadioButton
ddlSubjectMatter
spnTxtSubjectMatter
txtSubjectMatter
StepOneContinueButton
hdnBlockBack
hdnPhoneChecked
hdnCompanyChecked
hdnPhoneNumber
<小时>
这是版本号.
Here are the version numbers.
tinkerboard$ python3 --version
Python 3.5.3
tinkerboard$ /usr/bin/chromium --version
Chromium 72.0.3626.122 built on Debian 9.8, running on Debian 9.8
tinkerboard$ /usr/bin/chromedriver --version
ChromeDriver 72.0.3626.122
推荐答案
问题似乎是,Chrome 浏览器的名称在不同操作系统和平台上是一个移动目标.这段代码在Linux上避免了x86_64和ARM上的异常:
The problem seems to be, the name of the Chrome browser is a moving target on different OSes and platforms. This code has avoided the exception on x86_64 and ARM on Linux:
def get_chrome():
"""
Helper function to locate chrome browser.
"""
if os.path.isfile('/usr/bin/chromium-browser'):
return '/usr/bin/chromium-browser'
if os.path.isfile('/usr/bin/chromium'):
return '/usr/bin/chromium'
if os.path.isfile('/usr/bin/chrome'):
return '/usr/bin/chrome'
if os.path.isfile('/usr/bin/google-chrome'):
return '/usr/bin/google-chrome'
return None
然后像这样使用它:
if version.parse(selenium.__version__) >= version.parse("3.0"):
opts = Options()
opts.binary_location = get_chrome()
opts.add_argument('--headless')
opts.add_argument('--no-sandbox')
opts.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(chrome_options=opts)
driver.maximize_window()
else:
profile = webdriver.ChromeProfile()
profile.headless = True
profile.binary_location = get_chrome()
driver = webdriver.Chrome(profile)
driver.maximize_window()
这篇关于通过 xpath 枚举元素时的 Selenium chromedriver 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!