使用Python绑定将chromeOptions传递给RemoteDriver

时间:2014-10-15 14:29:40

标签: python selenium-webdriver webdriver

我尝试为Chrome样式的RemoteDriver添加一些chromeOptions。

我已经通过复制现有的Chrome词典创建了DesiredCapabilities词典,并且我尝试使用ChromeOptions()对象添加我自己的选项。

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

class RemoteDriverStartService():

    chrome = DesiredCapabilities.CHROME.copy()
    options = webdriver.ChromeOptions()
    options.add_experimental_option("Proxy", "null")
    options.add_experimental_option("excludeSwitches", ["ignore-certificate-errors"])
    chrome["chrome.options"] = options

    def start_driver(self):
        return webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', 
                                desired_capabilities=self.chrome)

上面引发了一个异常,即JSON无法序列化。我认为这是由于chrome["chrome.options"]行,但我无法解决我需要使用的内容。

1 个答案:

答案 0 :(得分:3)

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

class RemoteDriverStartService():

    options = webdriver.ChromeOptions()
    options.add_experimental_option("Proxy", "null")
    options.add_experimental_option("excludeSwitches", ["ignore-certificate-errors"])
    caps = options.to_capabilities()  # Here's the interesting part :)

    def start_driver(self):
        return webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', 
                                desired_capabilities=self.caps)