【问题标题】:set chrome options with remote driver使用远程驱动程序设置 chrome 选项
【发布时间】:2012-10-25 00:11:12
【问题描述】:

所以有一个不错的 long list of switches 可以传递给 chromedriver。

我想使用其中的一些,特别是--disable-logging

我不想(仅)在本地使用 chromedriver,我想编写所有代码以使用 webdriver.Remote()

这是我用来设置 chrome 驱动程序的代码,它适用于 vanilla chrome 实例。

driver = webdriver.Remote(
    command_executor = 'http://127.0.0.1:4444/wd/hub',
    desired_capabilities = {
        'browserName': 'chrome',
    }
)

但是我不知道如何传递其他选项。

当我查看driver.capabilities 时,我看到以下内容

{
    u'rotatable': False,
    u'browserConnectionEnabled': False,
    u'acceptSslCerts': False,
    u'cssSelectorsEnabled': True,
    u'javascriptEnabled': True,
    u'nativeEvents': True,
    u'databaseEnabled': False,
    u'chrome.chromedriverVersion': u'23.0.1240.0',
    u'locationContextEnabled': False,
    u'takesScreenshot': True,
    u'platform': u'MAC',
    u'browserName': u'chrome',
    u'webdriver.remote.sessionid': u'1352096075502',
    u'version': u'22.0.1229.94',
    u'applicationCacheEnabled': False,
    u'webStorageEnabled': True,
    u'handlesAlerts': True,
    u'chrome.nativeEvents': False
}

我不see any other arguments(除了desired_capabilities)通过webdriver.Remote 将参数传递给chromedriver。这是真的?我错过了什么吗?还有其他自定义chromedriver的策略吗?

CromeDrive wiki 页面上有一个很好的示例,显示 "Starting Chromium with Specific Flags",但所有示例均针对 webdriver.Chrome();该示例也在 java 中,因此它甚至可能无法在 python 中使用。

如果有人让这个工作或可以告诉我这只是行不通,我将不胜感激。谢谢。

新问题

我不确定处理后续问题的最佳方式。

所以,我得到了问题的答案,但我仍然无法关闭日志记录。检查以下记录器行。

[0.455][INFO]:      Launching chrome: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome --enable-logging --log-level=1 --disable-hang-monitor --disable-prompt-on-repost --dom-automation --full-memory-crash-report --no-default-browser-check --no-first-run --disable-background-networking --disable-sync --disable-translate --disable-web-resources --safebrowsing-disable-auto-update --safebrowsing-disable-download-protection --disable-client-side-phishing-detection --disable-component-update --disable-default-apps --use-mock-keychain --ignore-certificate-errors --disable-logging about:blank

我可以将参数 --disable-logging 传递给 chromedriver,但它似乎只关心启用日志记录的第一个参数。我想我需要找出 Chrome 新实例的默认参数保存在哪里。

【问题讨论】:

    标签: python selenium webdriver selenium-chromedriver


    【解决方案1】:

    这应该会给你可用的标志:

    from selenium import webdriver
    options = webdriver.ChromeOptions()
    # set some options
    # for example:
    # options.add_argument('--disable-logging')
    driver = webdriver.Remote(desired_capabilities=options.to_capabilities())
    

    【讨论】:

    • 看起来你需要做 `options.add_argument('--disable-logging')。
    • @Nathan Villaescusa -- 是的,我将添加一个示例来说明。谢谢。
    • 这将导致desired_capabilities 成为{"chromeOptions": {"args":['--disable-logging']}},这与我的回答不同。看看哪一个最终起作用会很有趣。
    • 这行得通(用于传递额外的参数),非常感谢。不过我遇到了一个新问题。
    【解决方案2】:

    自从 selenium 远程和 chrome 网络驱动程序发生变化后,我只需花两分钱。

    import os
    from selenium import webdriver
    
    
    class RemoteBrowser:
    
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument('whitelisted-ips')
        chrome_options.add_argument('headless')
        chrome_options.add_argument('no-sandbox')
        chrome_options.add_argument('window-size=1200x800')
    
        def __init__(self):
            self.hub_url = os.environ['HUB_URL']
            self.driver = webdriver.Remote(
                command_executor='http://' + self.hub_url + '/wd/hub',
                desired_capabilities = {'browserName': 'chrome'},
                options=self.chrome_options
            )
    

    【讨论】:

    • 确认在 selenium-server-standalone-3.141.59.jar 和 ChromeDriver 75.0.3770.100 上工作。其他答案无效。
    【解决方案3】:

    从源代码看来,唯一可行的方法是通过desired_capabilities 传递它。这个字典是 sent directly to the remove driver 通过 POST 请求。

    在查看了如何使用特定标志启动 chromium 之后,也许这样的事情会起作用:

    desired_capabilities = {
        'browserName': 'chrome',
        'chrome.switches': ['--disable-logging']
    }
    

    【讨论】:

    • 顺便说一句,这也有效,不过我更喜欢使用webdriver.ChromeOptions(),看起来更简洁、更明确。
    • 是的,我可能也会使用这种方法。
    • (意识到已经有好几年了......)这对我不起作用。相反,在传递带有数组的功能字典(作为 webdriver.Remote() 的参数)后出现此错误:“selenium.common.exceptions.WebDriverException:消息:无法将数组转换为字符串。”
    【解决方案4】:

    ChromeOptions() 用于传递附加参数。试试这个来禁用 chromedriver.log

    driver = webdriver.Chrome(service_log_path='/dev/null')
    

    【讨论】:

    • 这不是使用“远程”网络驱动程序
    猜你喜欢
    • 2014-10-03
    • 1970-01-01
    • 2019-09-22
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    • 2020-07-09
    • 1970-01-01
    相关资源
    最近更新 更多