Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Selenium Webdriver opens new window for every time run the script and how to fix it?

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

like image 623
b.sachin Avatar asked Oct 18 '25 12:10

b.sachin


2 Answers

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);
    
like image 120
undetected Selenium Avatar answered Oct 20 '25 02:10

undetected Selenium


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

driver.Quit();

and that should close it and clean up.

like image 25
MWS Avatar answered Oct 20 '25 03:10

MWS