Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium chromedriver won't launch URL if another chrome instance is open

Tags:

selenium

I tried to load chrome profile using selenium weDriver. The profile loads fine but it failed when it tries to load the URL.

I noticed that this issue happens when there is another chrome instance open whether or not it was open by webDriver. I have selenium 2.53.1.

System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/useName/AppData/Local/Google/Chrome/User Data");
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);

driver.get("www.google.com") // here is where it fails. It works fine if I close all chrome browsers before I run the test
like image 424
SOAlgorithm Avatar asked Jul 05 '16 00:07

SOAlgorithm


People also ask

Can we use Selenium to work with an already open browser session?

We can connect to an already open browser using Selenium webdriver. This can be done using the Capabilities and ChromeOptions classes. The Capabilities class obtains the browser capabilities with the help of the getCapabilities method.

How do I access the URL in Selenium?

Click on the “console” tab. In the console, type “document. URL” command and hit enter. You will give the current URL.


1 Answers

I found a workaround for this issue. I noticed that this issue happens because chromedriver will not be able to launch with the same profile if there is another open instance using the same profile. For example, if chrome.exe is already open with the default profile, chromedriver.exe will not be able to launch the default profile because chrome.exe is already open and using the same profile.

To fix this, you will need to create a separate profile for automation by copying the default profile so that chromedriver.exe and chrome.exe don't share the same default profile.

The default chrome profile is in this location:

C:\Users\yourUserName\AppData\Local\Google\Chrome\User Data\

Copy all files from User Data folder to a new folder and call it AutomationProfile

After you copy the files to the new folder then you can use it for your scripts.

        String userProfile= "C:\\Users\\YourUserName\\AppData\\Local\\Google\\Chrome\\AutomationProfile\\";
        ChromeOptions options = new ChromeOptions();
        options.addArguments("user-data-dir="+userProfile);
        options.addArguments("--start-maximized");

        driver = new ChromeDriver(options);

Make sure you use driver.quit() at the end of your test so that you don't keep chromedriver.exe open

like image 149
SOAlgorithm Avatar answered Sep 20 '22 13:09

SOAlgorithm