无法在 Amazon Linux 上运行 Chrome



我有一个python脚本,可以启动一个无头的Web驱动程序。完全相同的代码在 CentOS7 上没有问题。我正在尝试在亚马逊Linux上运行它

谷歌浏览器 --版本输出Google Chrome 83.0.4103.61

Chrome驱动程序 --版本输出ChromeDriver 83.0.4103.39

这是函数

def LoadWebDrivers():
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--start-maximized')
options.add_argument('--window-size=800,600')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--remote-debugging-port=443')
global WebDriver
WebDriver = webdriver.Chrome(executable_path=driverPath,options=options)
WebDriver.get('site')
return WebDriver

如果我直接从 CLI 运行 chrome,我会得到这个(还有一堆我读到的 Fontconfig 警告可以忽略(

[3779:3779:0520/015819.096591:ERROR:browser_main_loop.cc(1473)] Unable to open X display.
[0520/015819.108227:ERROR:nacl_helper_linux.cc(308)] NaCl helper process running without a sandbox!
Most likely you need to configure your SUID sandbox correctly
Segmentation fault

脚本因此而失败

File "/home/ec2-user/.local/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__
RemoteWebDriver.__init__(
File "/home/ec2-user/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/home/ec2-user/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/ec2-user/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/ec2-user/.local/lib/python3.8/site-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

启用日志记录后,这是日志文件吐出的内容(devtools 错误的多个条目(

[0520/021834.142267:ERROR:socket_posix.cc(148)] bind() failed: Permission denied (13)
[0520/021834.143153:ERROR:socket_posix.cc(148)] bind() failed: Permission denied (13)
[0520/021834.143191:ERROR:devtools_http_handler.cc(298)] Cannot start http server for devtools.
[1589941174.014][DEBUG]: DevTools HTTP Request: http://localhost:443/json/version
[1589941174.015][DEBUG]: DevTools HTTP Request failed
[1589941174.076][INFO]: [1d7ba4dc7a13a3e6bce467b1e3c51393] RESPONSE InitSession ERROR chrome not reachable
[1589941174.076][DEBUG]: Log type 'driver' lost 0 entries on destruction
[1589941174.076][DEBUG]: Log type 'browser' lost 0 entries on destruction

除了上述设置之外,还可以尝试使用以下设置。还要在详细级别添加一些日志记录。

chromedriver_path = "<chromedriver_binary_path>"
outputdir = "<log_dir_path>"
service_log_path = "{}/chromedriver.log".format(outputdir)
service_args = ['--verbose']
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(executable_path=chromedriver_path, service_log_path=service_log_path, service_args=service_args, options=chrome_options)
driver.get(url)

查看日志错误404后,要求我添加并查看

[0520/021834.142267:ERROR:socket_posix.cc(148)] bind() failed: Permission denied (13)
[0520/021834.143153:ERROR:socket_posix.cc(148)] bind() failed: Permission denied (13)
[0520/021834.143191:ERROR:devtools_http_handler.cc(298)] Cannot start http server for devtools.

我以 sudo 的形式运行脚本,它现在成功加载。

相关内容

  • 没有找到相关文章

最新更新