列举与XPath元素时硒chromedriver异常(Selenium chromedriver e

2019-09-27 01:55发布

我想一个脚本硒从Firefox转换到Chrome浏览器。 该代码按预期工作与Firefox和geckodriver在x86_64 。 geckodriver不支持ARM很好,所以我试图移动Chrome浏览器。

铬和chromedriver使用时引起异常driver.find_elements_by_xpath('//*[@id]') 唯一的例外是selenium.common.exceptions.WebDriverException: Message: chrome not reachable

这是什么问题,如何解决?


下面是测试程序。

$ 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时除外。

$ ./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)

此代码:

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

下面是版本号。

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
文章来源: Selenium chromedriver exception when enumerating elements by xpath