How to Connect Selenium To Existing Chrome Browser?
In this post, we will be talking about in detail, How to Connect Selenium to the Existing Chrome Browser which is manually opened in remote debugging mode. So let’s start with the following topic.
What is Remote Debugging In Chrome Browser?
In sort, Remote debugging is debugging of application that might be running on some other machine or device other than your local machine. This is always done by connecting the remotely running application with your developer environment.
This is a concept is particularly helpful when a device that cannot host the development platform and debugging of application is needed in real time. Such scenario could be any app on android mobile, or some application which is hosted on some dedicated server and we need to take control on application.
For such a scenario mainly in the browser, Chrome and Firefox have provided the option of debuggerAddress which can be used to take control of any browser open in remote machine using certain <hostname>:<port>. This debuggerAddress can be exploited in Selenium WebDriver Scripting as well.
So by using debuggerAddress, we can interact with already open chrome session through selenium script using capabilities.
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.
chrome.exe --remote-debugging-port=9222 --user-data-dir=remote-profile
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.
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
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:
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.
So This is How to Connect Selenium To Existing Chrome Browser?. Hope you find this post useful.
This is a fantastic blog from which people can learn a lot. It is very informative and is explained in…
Thanks for sharing such knowledgeable Content on Automation testing, to know how to enhance the performance of ERP's like Oracle…
Thanks.. this list is much needed.
This one is also good to use. Thanks for sharing this.. Might be we can also add it in list…
How about youtube-dl?