Selenium ChromeDriver On Dockers(Linux)无法关闭

时间:2018-02-28 12:50:02

标签: python-2.7 selenium docker debian selenium-chromedriver

当我这样做时

driver = webdriver.Chrome(binary_location,chrome_options=options)
driver.get(URL)
...
driver.close()

引发以下错误:

'chromedriver' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home

我在docker上使用python 2.7图像

将二进制更改为可执行文件

chmod a+x PATH_TO_DIR/chromedriver

它引发了以下错误

ERROR: Message: unknown error: failed to close window in 20 seconds
(Session info: content shell=)
(Driver info: chromedriver=2.35.528139 
(47ead77cb35ad2a9a83248b292151462a66cd881),platform=Linux 4.11.0-1013-azure x86_64)

1 个答案:

答案 0 :(得分:0)

错误说明了一切:

'chromedriver' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home

错误意味着在以下代码行中:

driver = webdriver.Chrome(binary_location,chrome_options=options)

程序期望在您传递的参数中有 chromedriver 可执行文件。但无法找到 chromedriver 可执行文件。

因此解决方案不是将binary_location作为参数传递,而是需要传递 chromedriver 二进制文件的绝对位置,如下所示:

driver = webdriver.Chrome(executable_path='/path/to/chromedriver', chrome_options=options)
driver.get(URL)

ChromeDriver 会从系统类路径中找出默认的binary_location并初始化 Chrome浏览器

更新

如果您仍希望明确地传递binary_location,则可以通过Options类传递 chrome 二进制文件的绝对位置,如下所示:

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

options = Options()
options.add_argument("start-maximized")
options.binary_location('/path/to/chrome')
driver = webdriver.Chrome(executable_path='/path/to/chromedriver', chrome_options=options)
driver.get('http://google.com/')