尝试在linux

时间:2018-02-17 12:05:52

标签: selenium selenium-chromedriver robotframework google-chrome-headless

我正在尝试使用Chrome在无头模式下在Linux机器上运行机器人框架selenium python测试。所有测试运行正常,直到我按下按钮下载文件。当我尝试下载文件时,抛出以下错误:

  

“WebDriverException:消息:chrome无法访问(会话信息:   headless chrome = 64.0.3282.140)(驾驶信息:chromedriver = 2.35.528139   (47ead77cb35ad2a9a83248b292151462a66cd881),平台= Linux的   4.9.27-14.31.amzn1.x86_64 x86_64)“

同样的测试在Windows中运行良好。

以下是我用来创建网络驱动程序会话的代码。

Launch Headless Chrome
    Create Download Directory
    ${chrome options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
    Call Method    ${chrome options}    add_argument    start-maximized
    Call Method    ${chrome_options}    add_argument    --headless
    Call Method    ${chrome_options}    add_argument    --disable-gpu
    Call Method    ${chrome_options}    add_argument    --window-size\=1920,1080
    Call Method    ${chrome_options}    add_argument    --lang\=en-us
    Call Method    ${chrome_options}    add_argument    --no-sandbox
    ${prefs}       Create Dictionary    download.default_directory=${download directory}    download.prompt_for_download=false   options.binary_location=/usr/bin/chromedriver
    Call Method    ${chrome options}    add_experimental_option    prefs    ${prefs}
    Create Webdriver    Chrome    chrome_options=${chrome options}
    Enable Download In Headless Chrome    ${download directory}
    Go To   about:blank

以下是用于启用下载行为和设置chrome headless的下载目录的代码。

def enable_download_in_headless_chrome(download_dir):
logger.info('Getting SeleniumLibrary Instance')
instance = BuiltIn().get_library_instance('SeleniumLibrary')
driver = instance.driver
# add missing support for chrome "send_command"  to selenium webdriver
driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')

params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': download_dir}}
command_result = driver.execute("send_command", params)
logger.info("response from browser:")
for key in command_result:
    logger.info("result:" + key + ":" + str(command_result[key]))

1 个答案:

答案 0 :(得分:0)

您需要在以下位置检查以下参数options.binary_location=/usr/bin/chromedriver

${prefs}       Create Dictionary    download.default_directory=${download directory}    download.prompt_for_download=false   options.binary_location=/usr/bin/chromedriver

Python 绑定中,它定义如下,并引用chrome.exe的位置:

@property
def binary_location(self):
    """
    Returns the location of the binary otherwise an empty string
    """
    return self._binary_location

options.binary_location=/usr/bin/chromedriver更改为 options.binary_location=/path/to/chrome