Selenium Python脚本:无法访问Chrome

pgky5nke  于 2023-02-04  发布在  Python
关注(0)|答案(1)|浏览(182)

我目前正在EC2机器上运行一个 selenium Python脚本,谷歌Chrome版本(100.0.4896.60)Chrome驱动程序(100.0.4896.60)红帽(8.7)Python(3.6.8) selenium (3.141.0)Pytest(7.0.1)

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

#argument to switch off suid sandBox and no sandBox in Chrome
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument("--remote-debugging-port=9222")
# chrome_options.add_argument('--start-maximized')
chrome_options.add_argument('--headless')
#chrome_options.add_argument("--window-size=1920,1080")
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-popup-blocking')
chrome_options.add_argument("--incognito")
userAgent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36"
chrome_options.add_argument(f'user-agent={userAgent}')
chrome_options.add_argument('ignore-certificate-errors')

driver = webdriver.Chrome("/usr/local/share/chromedriver", options=chrome_options)
driver.get('http://www.google.com')
print(driver.title)
driver.quit()

当我运行脚本时,我得到了以下错误。

================================================================================================================== ERRORS ===================================================================================================================
_________________________________________________________________________________________________________ ERROR collecting main.py __________________________________________________________________________________________________________
main.py:21: in <module>
    driver = webdriver.Chrome("/usr/local/share/chromedriver", options=chrome_options)
/usr/local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py:81: in __init__
    desired_capabilities=desired_capabilities)
/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py:157: in __init__
    self.start_session(capabilities, browser_profile)
/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py:252: in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py:321: in execute
    self.error_handler.check_response(response)
/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py:242: in check_response
    raise exception_class(message, screen, stacktrace)
E   selenium.common.exceptions.WebDriverException: Message: chrome not reachable
========================================================================================================== short test summary info ==========================================================================================================
ERROR main.py - selenium.common.exceptions.WebDriverException: Message: chrome not reachable
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

我的红帽服务器上有一个谷歌浏览器的更新,导致它升级到版本109,我被告知降级版本回到100,因此,避免任何中断我们的脚本在短期内。现在的版本谷歌 chrome 和chromedriver匹配,但我仍然看到错误, chrome 无法达到。我已经确保 chrome 驱动程序是在正确的路径以及正确的权限。我使用“chmod +x chromedriver”在谷歌浏览器上设置权限。不知道还能做什么。
这个脚本的预期结果非常简单,因为这不是我的主代码,而是一个例子。当在这台机器上运行任何脚本时,我都会得到同样的错误。

mftmpeh8

mftmpeh81#

你不需要所有这些参数。要启用headless模式,你只需要以下参数:

    • 一月一日 *
    • 一米一分一秒 *
    • 一米二米一 *

删除其他参数:

    • 一米三分一秒 *
    • 一米四分一秒 *
    • 一米五分一秒 *
    • 一米六分一秒 *
    • 一米七三 *
    • 一米八分一秒 *
    • 一米九十一 *
    • 一米十寸 *

并执行您的程序:

driver = webdriver.Chrome(executable_path=r'/usr/local/share/chromedriver', options=chrome_options)
driver.get('http://www.google.com')
driver = webdriver.Chrome(service=Service('/usr/local/share/chromedriver'), options=chrome_options)
driver.get('http://www.google.com')

相关问题