Connect Selenium To Existing Chrome Browser

What are the Scenarios When Remote Debugging In Required In Selenium WebDriver?

In Automation, We have plenty of scenarios where remote debugging is required but listing some of the scenario here and remaining if you like to mention you can add in comment to improved this scenario list.

1- Let’s say multiple team members are working to automate an end to end-user workflow, you are automating one or two pages in the end to end automation and you don’t have the complete script. In such a scenario, you can navigate to the page assigned to you, manually, from there you can continue with your automation script.

2- If captcha is implemented with some login credential that you don’t want to expose to anyone. In such scenario you can setup your login process manually and handover the session to automation script to use the same browser session.

3- If application hybrid where some of the part is coming from some API or desktop application and remaining part is web page which can be handled through Selenium.

4- What if some application is failing on end user mobile application and you want to deal with this scenario. In this case you can ask end user to share the browser session or application session using remote debugger address feature provided in chrome.

5- What if some click action is not working properly and you need to do manual intervention.

How to launch a chrome browser with a remote debugger address?

To launch a chrome browser in remote debug mode, you need to follow these steps.

1- Navigate to chrome.exe path through command line.

2- Type following line of code on command line.

 
1
chrome.exe remotedebuggingport=9222 userdatadir=remoteprofile

Let’s understand the parts of the above command

  • chrome.exe: Chrome Browser Binary ( This is will different in all other operating systems like Unix, Mac, Linux)
  • –remote-debugging-port: This is Chrome Preference to launch the browser in remote debug mode on a certain port, We can also use –remote-debugging-address.
  • –user-date-dir: this is a directory where the browser stores the user profile, So we should always provide a new profile directory to save your default browser profile preferences.

3- Navigate to the page where you want to start your debugging.

4- In browser, type <machine ip>:9222.

This will open the page on your machine using remote debugging mode.

Chrome Browser Remote Debugging mode.

How to Connect Selenium To Existing Chrome Browser?

In Selenium, ChromeOptions are used to interact with already open chrome browser in remote debug mode.

line of code used

 
1
2
3
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption(“debuggerAddress”, “127.0.0.1:9222”);
WebDriver driver = new ChromeDriver(options);

Scenario:
1- Open the Command line and run this command on windows machine
chrome.exe –remote-debugging-port=9222 –user-data-dir=”D:\selenium\RemoteProfile”
2- In the newly launched browser, Open https://abodeqa.com
3- Click on Contact Us Page.
4- Get back to the selenium script and run it. ( I have already written one code which will enter text in all the field on contact us page.)

Here is the complete code:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package webdriverExample;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class ChromeRemoteDebuggingUsingSelenium {
public static void main(String[] args) {
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption(“debuggerAddress”, “127.0.0.1:9222”);
WebDriver driver = new ChromeDriver(options);
//name field on contact us page of abodeqa
driver.findElement(By.id(“g1280-name”)).sendKeys(“abodeqa”);
//Email id field
driver.findElement(By.id(“g1280-email”)).sendKeys(“abodeqa@gmail.com”);
//website
driver.findElement(By.id(“g1280-website”)).sendKeys(“https://abodeqa.com”);
//comment.
driver.findElement(By.id(“contact-form-comment-g1280-comment”)).sendKeys(“This is one sample comment.”);
}
}

 

Above code will use the same chrome session, which is launched using step 1 and continue from step 4.

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *