splinter: how to add chrome options? splinter: how to add chrome options? selenium selenium

splinter: how to add chrome options?


Create a new instance of BaseWebDriver and set .driver with an instance of the Chrome driver. This example starts Chrome maximized:

from selenium.webdriver import Chromefrom selenium.webdriver.chrome.options import Optionsfrom splinter.driver.webdriver import BaseWebDriver, WebDriverElementoptions = Options()options.add_argument('--start-maximized')browser = BaseWebDriver()browser.driver = Chrome(chrome_options=options)browser.visit('https://www.google.com')


The only way I could ever do this was by using the add_argument method with selenium.webdriver.ChromeOptions like so:

from selenium.webdriver import ChromeOptionsfrom splinter import Browserchrome_options = ChromeOptions()chrome_options.add_argument(your_argument)b=Browser("chrome", options=chrome_options)b.visit('http://www.google.com')b.quit()

so in your code would be:

from splinter import Browserfrom selenium.webdriver import ChromeOptionsfrom pyvirtualdisplay import Display #I'm not certain what this is...d = Display(visible=0, size=(800, 600))d.start()chrome_options = ChromeOptions()chrome_options.add_argument('disable-setuid-sandbox')b = Browser('chrome')b.visit('http://www.google.com')b.quit()d.stop()

Note: I was unable to test this with your argument specifically because I recently broke my GRUB so I am stuck in windows, and the disable-setuid-sandbox option is linux-only. However, I have been using this method with the headless argument for a while.


I am not 100% sure that this will work but I just looked at the docs for splinter and it says.

You can also pass additional arguments that correspond to Selenium DesiredCapabilities arguments.

Looking into the sourcecode of Splinter calling Browser can take some arguments. These arguments will then be passed to create an Instance of the Chrome WebDriver. So I went to the selenium sourcecode and saw the constructor looks like this:

def __init__(self, executable_path="chromedriver", port=0,                 chrome_options=None, service_args=None,                 desired_capabilities=None, service_log_path=None):

There is a parameter for chrome_options so it should be possible to pass it using this parameter. So if I'm correct this should work fine:

opt = Options()opt.add_argument('--disable-setuid-sandbox')b = Browser(browser='chrome', chrome_options=opt)

EditAlternatively you could pass the options as desired capabilities aswell:

opt = Options()opt.add_argument('--disable-setuid-sandbox')dc = opt.to_capabilities()b = Browser(browser='chrome', desired_capabilities=dc)