Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

splinter: how to add chrome options?

I'm using splinter(v0.7.3) for web testing under linux, while on chrome, the default sample code can not running:

from splinter import Browser
from pyvirtualdisplay import Display

d = Display(visible=0, size=(800, 600))
d.start()

b = Browser('chrome')
b.visit('http://www.google.com')
b.quit()

d.stop()

While running, I got the exception like this:

selenium.common.exceptions.WebDriverException: Message: chrome not reachable

And I test the same function in selenium with some chrome option added:

from selenium import web driver
from selenium.webdriver.chrome.options import Options
from pyvirtualdisplay import Display

d = Display(visible=0, size=(800, 600))
d.start()

opt = Options()
opt.add_argument('--disable-setuid-sandbox')
b = webdriver.Chrome(chrome_options=opt)
b.get('http://www.google.com')
b.quit()

d.stop()

This works ok, the difference is the --disable-setuid-sandbox option added to chrome driver, if the option not add, there would be a zombie chrome-sandbox process under chromium-browser.

The problem here is, I don't know how to pass a chrome.options.Option instance to splinter.Browser(), I browsed the implementation under splinter/driver/webdriver/chrome.py, it seems that there is no entry to pass such a instance to splinter.Browser(). Is there some other way to pass options to chrome driver?

like image 321
coanor Avatar asked Oct 19 '22 08:10

coanor


1 Answers

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 Chrome
from selenium.webdriver.chrome.options import Options
from splinter.driver.webdriver import BaseWebDriver, WebDriverElement

options = Options()
options.add_argument('--start-maximized')

browser = BaseWebDriver()
browser.driver = Chrome(chrome_options=options)

browser.visit('https://www.google.com')
like image 60
Florent B. Avatar answered Jan 04 '23 07:01

Florent B.