Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add selenium chrome options to 'desiredCapabilities'?

For selenium I have a bunch of options for chrome, which I need to pass to the remote webdriver via DesiredCapabilities. On this page there is a java example on how to do this, but how to do it in python? The documentation is very poor.

Here is the code I have so far:

prefs = {
    "profile.default_content_settings.popups":0,
    "download.prompt_for_download": "false",
    "download.default_directory": cwd,
}
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_experimental_option("prefs", prefs) 

capabilities = DesiredCapabilities.CHROME

#code I could not find 
#I need something like
#capabilities.add_options(chrome_options)

driver = webdriver.Remote(
            command_executor='http://aaa.bbb.ccc:4444/wd/hub',
            desired_capabilities=capabilities)

Any idea ho to do this? Or where to find proper documentation?

like image 994
Alex Avatar asked Nov 02 '17 12:11

Alex


People also ask

How do I add Chrome options to Selenium?

Creating an instance of ChromeOptions class: See below code: ChromeOptions options = new ChromeOptions(); options. addArguments("disable-infobars"); ChromeDriver driver = new ChromeDriver(options);

How do I pass Chrome options as capabilities?

Using the ChromeOptions class You can create an instance of ChromeOptions, which has convenient methods for setting ChromeDriver-specific capabilities. You can then pass the ChromeOptions object into the ChromeDriver constructor: ChromeOptions options = new ChromeOptions();

How can I include ChromeDriver in a jar?

Export your code as jar without the chromedriver. Create a folder ChromeDriver. Place your chromedriver.exe in this folder. Place ChromeDriver folder along with your jar.

What is the difference between ChromeOptions and DesiredCapabilities?

ChromeOptions class has introduced in the latest/updated version of Selenium. It is helpful to make changes in the Chrome browser whereas, DesiredCapabilities is an old concept (its usage in Java is deprecated.) to configure or make changes in the browser. Save this answer.


1 Answers

Use options.to_capabilities() to get the capabilities from the options:

options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("--disable-gpu")

capabilities = options.to_capabilities()

driver = webdriver.Remote( \
  command_executor='http://127.0.0.1:4444/wd/hub', \
  desired_capabilities=capabilities)
like image 76
Florent B. Avatar answered Sep 29 '22 04:09

Florent B.