Browser Options

These capabilities are shared by all browsers.

In Selenium 3, capabilities were defined in a session by using Desired Capabilities classes. As of Selenium 4, you must use the browser options classes. For remote driver sessions, a browser options instance is required as it determines which browser will be used.

These options are described in the w3c specification for Capabilities.

Each browser has custom options that may be defined in addition to the ones defined in the specification.

browserName

Browser name is set by default when using an Options class instance.

72
75
Show full example
package dev.selenium.drivers;

import dev.selenium.BaseTest;

import java.time.Duration;
import java.time.temporal.ChronoUnit;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.chrome.ChromeDriver;

public class OptionsTest extends BaseTest {

  @Test
  public void setPageLoadStrategyNormal() {
    ChromeOptions chromeOptions = getDefaultChromeOptions();
    chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);
    WebDriver driver = new ChromeDriver(chromeOptions);
    try {
      // Navigate to Url
      driver.get("https://selenium.dev");
    } finally {
      driver.quit();
    }
  }

  @Test
  public void setPageLoadStrategyEager() {
    ChromeOptions chromeOptions = getDefaultChromeOptions();
    chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);
    WebDriver driver = new ChromeDriver(chromeOptions);
    try {
      // Navigate to Url
      driver.get("https://selenium.dev");
    } finally {
      driver.quit();
    }
  }

  @Test
  public void setPageLoadStrategyNone() {
    ChromeOptions chromeOptions = getDefaultChromeOptions();
    chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);
    WebDriver driver = new ChromeDriver(chromeOptions);
    try {
      // Navigate to Url
      driver.get("https://selenium.dev");
    } finally {
      driver.quit();
    }
  }

  @Test
  public void setAcceptInsecureCerts() {
    ChromeOptions chromeOptions = getDefaultChromeOptions();
    chromeOptions.setAcceptInsecureCerts(true);
    WebDriver driver = new ChromeDriver(chromeOptions);
    try {
      // Navigate to Url
      driver.get("https://selenium.dev");
    } finally {
      driver.quit();
    }
  }

  @Test
  public void getBrowserName() {
	ChromeOptions chromeOptions = getDefaultChromeOptions();
	String name = chromeOptions.getBrowserName();
	Assertions.assertFalse(name.isEmpty(), "Browser name should not be empty");
  }

  @Test
  public void setBrowserVersion() {
	ChromeOptions chromeOptions = getDefaultChromeOptions();
	String version = "latest";
	chromeOptions.setBrowserVersion(version);
	Assertions.assertEquals(version, chromeOptions.getBrowserVersion());
  }

  @Test
  public void setPlatformName() {
	ChromeOptions chromeOptions = getDefaultChromeOptions();
	String platform = "OS X 10.6";
	chromeOptions.setPlatformName(platform);
	Assertions.assertEquals(platform, chromeOptions.getPlatformName().toString());
  }

  @Test
  public void setScriptTimeout() {
	ChromeOptions chromeOptions = getDefaultChromeOptions();
	Duration duration = Duration.of(5, ChronoUnit.SECONDS);
	chromeOptions.setScriptTimeout(duration);

	WebDriver driver = new ChromeDriver(chromeOptions);
	try {
	  Duration timeout = driver.manage().timeouts().getScriptTimeout();
	  Assertions.assertEquals(timeout, duration, "The script timeout should be set to 5 seconds.");
	} finally {
	  driver.quit();
	}
  }

  @Test
  public void setPageLoadTimeout() {
	ChromeOptions chromeOptions = getDefaultChromeOptions();
	Duration duration = Duration.of(5, ChronoUnit.SECONDS);
	chromeOptions.setPageLoadTimeout(duration);

	WebDriver driver = new ChromeDriver(chromeOptions);
	try {
	  Duration timeout = driver.manage().timeouts().getPageLoadTimeout();
	  Assertions.assertEquals(timeout, duration, "The page load timeout should be set to 5 seconds.");
	} finally {
	  driver.quit();
	}
  }

  @Test
  public void setImplicitWaitTimeout() {
	ChromeOptions chromeOptions = getDefaultChromeOptions();
	Duration duration = Duration.of(5, ChronoUnit.SECONDS);
	chromeOptions.setImplicitWaitTimeout(duration);

	WebDriver driver = new ChromeDriver(chromeOptions);
	try {
	  Duration timeout = driver.manage().timeouts().getImplicitWaitTimeout();
	  Assertions.assertEquals(timeout, duration, "The implicit wait timeout should be set to 5 seconds.");
	} finally {
	  driver.quit();
	}
  }

  @Test
  public void setUnhandledPromptBehaviour() {
	ChromeOptions chromeOptions = getDefaultChromeOptions();
	chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);
	//verify the capability object is not null
	Object capabilityObject = chromeOptions.getCapability(CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR);
	Assertions.assertNotNull(capabilityObject, "Capability UNHANDLED_PROMPT_BEHAVIOUR should not be null.");
	Assertions.assertEquals(capabilityObject.toString(), UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY.toString());
  }

  @Test
  public void setWindowRect() {
   	ChromeOptions chromeOptions = getDefaultChromeOptions();
   	chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT, true);
   	//verify the capability object is not null
   	Object capabilityObject = chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);
    Assertions.assertNotNull(capabilityObject, "Capability SET_WINDOW_RECT should not be null.");

    Boolean capability = (Boolean) capabilityObject;
    Assertions.assertTrue(capability, "The capability SET_WINDOW_RECT should be set to true.");
  }
	
  @Test
  public void setStrictFileInteractability() {
    ChromeOptions chromeOptions = getDefaultChromeOptions();
    chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY, true);
	//verify the capability object is not null
    Object capabilityObject = chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);
    Assertions.assertNotNull(capabilityObject, "Capability STRICT_FILE_INTERACTABILITY should not be null.");

    Boolean capability = (Boolean) capabilityObject;
    Assertions.assertTrue(capability, "The capability STRICT_FILE_INTERACTABILITY should be set to true.");
  }
}

78
81
<details class="mt-3">
  <summary>Show full example</summary>
  <div class="pt-2">
    <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-py" data-lang="py"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">from</span> <span style="color:#000">selenium</span> <span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">webdriver</span>

from selenium.webdriver.common.proxy import Proxy from selenium.webdriver.common.proxy import ProxyType def test_page_load_strategy_normal(): options = get_default_chrome_options() options.page_load_strategy = 'normal' driver = webdriver.Chrome(options=options) driver.get("https://www.selenium.dev/") driver.quit() def test_page_load_strategy_eager(): options = get_default_chrome_options() options.page_load_strategy = 'eager' driver = webdriver.Chrome(options=options) driver.get("https://www.selenium.dev/") driver.quit() def test_page_load_strategy_none(): options = get_default_chrome_options() options.page_load_strategy = 'none' driver = webdriver.Chrome(options=options) driver.get("https://www.selenium.dev/") driver.quit() def test_timeouts_script(): options = get_default_chrome_options() options.timeouts = { 'script': 5000 } driver = webdriver.Chrome(options=options) driver.get("https://www.selenium.dev/") driver.quit() def test_timeouts_page_load(): options = get_default_chrome_options() options.timeouts = { 'pageLoad': 5000 } driver = webdriver.Chrome(options=options) driver.get("https://www.selenium.dev/") driver.quit() def test_timeouts_implicit_wait(): options = get_default_chrome_options() options.timeouts = { 'implicit': 5000 } driver = webdriver.Chrome(options=options) driver.get("https://www.selenium.dev/") driver.quit() def test_unhandled_prompt(): options = get_default_chrome_options() options.unhandled_prompt_behavior = 'accept' driver = webdriver.Chrome(options=options) driver.get("https://www.selenium.dev/") driver.quit() def test_set_window_rect(): options = webdriver.FirefoxOptions() options.set_window_rect = True # Full support in Firefox driver = webdriver.Firefox(options=options) driver.get("https://www.selenium.dev/") driver.quit() def test_strict_file_interactability(): options = get_default_chrome_options() options.strict_file_interactability = True driver = webdriver.Chrome(options=options) driver.get("https://www.selenium.dev/") driver.quit() def test_proxy(): options = get_default_chrome_options() options.proxy = Proxy({ 'proxyType': ProxyType.MANUAL, 'httpProxy' : 'http.proxy:1234'}) driver = webdriver.Chrome(options=options) driver.get("https://www.selenium.dev/") driver.quit()
def test_set_browser_name(): options = get_default_chrome_options() assert options.capabilities['browserName'] == 'chrome' driver = webdriver.Chrome(options=options) driver.get("https://www.selenium.dev/") driver.quit()
def test_set_browser_version(): options = get_default_chrome_options() options.browser_version = 'stable' assert options.capabilities['browserVersion'] == 'stable' driver = webdriver.Chrome(options=options) driver.get("https://www.selenium.dev/") driver.quit()
def test_platform_name(): options = get_default_chrome_options() options.platform_name = 'any' driver = webdriver.Chrome(options=options) driver.get("https://www.selenium.dev/") driver.quit()
def test_accept_insecure_certs(): options = get_default_chrome_options() options.accept_insecure_certs = True driver = webdriver.Chrome(options=options) driver.get("https://www.selenium.dev/") driver.quit() def get_default_chrome_options(): options = webdriver.ChromeOptions() options.add_argument("–no-sandbox") return options

<div class="text-end pb-2 mt-2">
  <a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full/examples/python/tests/drivers/test_options.py#L79-80" target="_blank">
    <i class="fas fa-external-link-alt pl-2"></i>
    <strong>View full example on GitHub</strong>
  </a>
</div>
10
12
Show full example
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Chrome' do
  describe 'Driver Options' do
    let(:chrome_location) { driver_finder && ENV.fetch('CHROME_BIN', nil) }
    let(:url) { 'https://www.selenium.dev/selenium/web/' }

    it 'page load strategy normal' do
      options = Selenium::WebDriver::Options.chrome
      options.page_load_strategy = :normal

      driver = Selenium::WebDriver.for :chrome, options: options
      driver.get(url)
      driver.quit
    end

    it 'page load strategy eager' do
      options = Selenium::WebDriver::Options.chrome
      options.page_load_strategy = :eager

      driver = Selenium::WebDriver.for :chrome, options: options
      driver.get(url)
      driver.quit
    end

    it 'page load strategy none' do
      options = Selenium::WebDriver::Options.chrome
      options.page_load_strategy = :none

      driver = Selenium::WebDriver.for :chrome, options: options
      driver.get(url)
      driver.quit
    end

    it 'sets remote capabilities', skip: 'this is example code that will not execute' do
      options = Selenium::WebDriver::Options.firefox
      options.platform_name = 'Windows 10'
      options.browser_version = 'latest'
      cloud_options = {}
      cloud_options[:build] = my_test_build
      cloud_options[:name] = my_test_name
      options.add_option('cloud:options', cloud_options)
      driver = Selenium::WebDriver.for :remote, capabilities: options
      driver.get(url)
      driver.quit
    end

    it 'accepts untrusted certificates' do
      options = Selenium::WebDriver::Options.chrome
      options.accept_insecure_certs = true

      driver = Selenium::WebDriver.for :chrome, options: options
      driver.get(url)
      driver.quit
    end

    it 'sets unhandled prompt behavior' do
      options = Selenium::WebDriver::Options.chrome
      options.unhandled_prompt_behavior = :accept

      driver = Selenium::WebDriver.for :chrome, options: options
      driver.get(url)
      driver.quit
    end

    it 'sets window rect' do
      options = Selenium::WebDriver::Options.firefox
      options.set_window_rect = true

      driver = Selenium::WebDriver.for :firefox, options: options
      driver.get(url)
      driver.quit
    end

    it 'sets strict file interactability' do
      options = Selenium::WebDriver::Options.chrome
      options.strict_file_interactability = true

      driver = Selenium::WebDriver.for :chrome, options: options
      driver.get(url)
      driver.quit
    end

    it 'sets the proxy' do
      options = Selenium::WebDriver::Options.chrome
      options.proxy = Selenium::WebDriver::Proxy.new(http: 'myproxy.com:8080')

      driver = Selenium::WebDriver.for :chrome, options: options
      driver.get(url)
      driver.quit
    end

    it 'sets the implicit timeout' do
      options = Selenium::WebDriver::Options.chrome
      options.timeouts = {implicit: 1}

      driver = Selenium::WebDriver.for :chrome, options: options
      driver.get(url)
      driver.quit
    end

    it 'sets the page load timeout' do
      options = Selenium::WebDriver::Options.chrome
      options.timeouts = {page_load: 400_000}

      driver = Selenium::WebDriver.for :chrome, options: options
      driver.get(url)
      driver.quit
    end

    it 'sets the script timeout' do
      options = Selenium::WebDriver::Options.chrome
      options.timeouts = {script: 40_000}

      driver = Selenium::WebDriver.for :chrome, options: options
      driver.get(url)
      driver.quit
    end

    it 'sets capabilities in the pre-selenium 4 way', skip: 'this is example code that will not execute' do
      caps = Selenium::WebDriver::Remote::Capabilities.firefox
      caps[:platform] = 'Windows 10'
      caps[:version] = '92'
      caps[:build] = my_test_build
      caps[:name] = my_test_name
      driver = Selenium::WebDriver.for :remote, url: cloud_url, desired_capabilities: caps
      driver.get(url)
      driver.quit
    end
  end
end