4

I am trying to run following code. But everytime I run a new chrome window opens. How to fix this problem. Also the new chrome instance that open are not logged in so I am unable to use the extension that i have already installed. Any way to fix this problem?

Any help will be appreciated.

I have provided screenshot for the same:

new chrome instances

Code trials:

package com.webdriver;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class WebElements {

    public static void main(String[] args) {

        WebDriver driver = new ChromeDriver();
        driver.get("http://www.google.com");

    }

}

base instance . . new instance not logged in

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

3 Answers3

2

When you execute your code each time a new ChromeDriver instance is created which spawns a new Chrome Browser session. This functionality is as per design.

A better approach would be to invoke driver.quit() within tearDown(){} method to close & destroy both the WebDriver and Web Browser instances gracefully after each run of Test Execution. You can find a detailed discussion here.

Though that would solve the issue of multiple Chrome Browser windows still you won't be logged in within your new session. To get logged in you need to use a specific Chrome Profile and store the cookies and reuse them later. You will find a detailed discussion on how to create and open a new Chrome Profile in this discussion.

To use the extension that you have installed you need to pack/unpack them and use ChromeOptions class.


Chrome Extensions

Chrome extensions can be either packed or unpacked. Packed extensions are a single file with a .crx extension. Unpacked extensions are a directory containing the extension, including a manifest.json file.

To pack an unpacked extension, use the Pack button in chrome://extensions or use Chrome (Windows example):

chrome.exe --pack-extension=C:\path\to\unpacked\extension --pack-extension-key=C:\myext.pem 

See the extensions docs for other ways to do this that are more automation friendly. To unpack a packed extension, just unzip the file (you may need to rename the file from .crx to .zip for your zip utility to recognize it).

Using extensions through ChromeDriver

  • Packed (.crx file):

    ChromeOptions options = new ChromeOptions();
    options.addExtensions(new File("/path/to/extension.crx"));
    WebDriver driver = new ChromeDriver(options);
    
  • Unpacked (directory):

    ChromeOptions options = new ChromeOptions();
    options.addArguments("load-extension=/path/to/extension");
    WebDriver driver = new ChromeDriver(options);
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

If you just want the window to close at the end you can call

driver.Quit();

and that should close it and clean up.

MWS
  • 78
  • 3
  • 10
0

Using the latest Selnium 4 Java jars and Selenium server jar resolved my issue of extra Chrome browser window

https://www.selenium.dev/downloads/

Mathew
  • 1
  • 1