Upgrading from Selenium 3.11.0 to 4.0.0 can't find Chrome Options setExperimentalOption method

Viewed 1346

I am trying to upgrade to Selenium 4. This error occurs: java.lang.NoSuchMethodError: org.openqa.selenium.chrome.ChromeOptions.setExperimentalOption(Ljava/lang/String;Ljava/lang/Object;)

Here is the code used to set the options:

WebDriver driver = new ChromeDriver(getChromeOptions());
  private ChromeOptions getChromeOptions() {
    try {
      ChromeOptions options = new ChromeOptions();

      HashMap<String, Object> chromePrefs = new HashMap<>();
      chromePrefs.put("profile.default_content_settings.popups", 0);
      chromePrefs.put("download.default_directory", fileLocation);
      options.setExperimentalOption("prefs", chromePrefs);
      options.addArguments("start-maximized");
      options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

      return options;
    } catch (Exception e) {
      LOG.error("Exception building options", e);
    }
    return null;
  }

Using chromedriver version 95.0.4638.54

I thought it upgrading would be as simple as changing the version number in the pom. I found this while researching:

In Selenium 4, DesiredCapabilities have been replaced by Options. To use the Driver class, QAs have to create an Options class, set test requirements, and pass the object to the Driver constructor.

I may implemented the above incorrectly. I am at a loss at this point. Any kind help would be greatly appreciated.

1 Answers

I tried to reproduce your issue but not. Earlier I used 4.0.0-alpha-7 version but upgraded it to 4.0.0 version and I tried and it's working. Below are the ChromeOptions of my current project.

ChromeOptions:

ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("start-maximized");


HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
chromeOptionsMap.put("plugins.plugins_disabled", new String[] { "Chrome PDF Viewer" });
chromeOptionsMap.put("plugins.always_open_pdf_externally", true);
chromeOptionsMap.put("download.default_directory", "C:\\test\\");
options.setExperimentalOption("prefs", chromeOptionsMap);

Maven dependency:

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.0.0</version>
    </dependency>

Code:

driver = new ChromeDriver(options);
driver.manage().deleteAllCookies();
driver.get("https://www.google.com");

Please try following steps:

  • Upgrade JDK to 1.8 or > 1.8
  • Clean your project workspace through your IDE and rebuild your project with required dependencies only.
Related