How to connect Selenium to an existing browser that was opened manually in java?
I want to achieve some use cases in which:
I want to perform some tasks that require manual intervention before kicking in automation execution. For instance, I need to manually launch the browser, navigate to the desired page, perform some manual tasks like entering captcha manually and then run the automation script to continue from there.
Debugging – Let's say we are working on a test script for a certain page (product checkout) in the application. But the script to reach that page (add to cart) is not ready yet. We can manually launch the browser and navigate to that page and then run the automation script.
Can someone help me to achieve this type of scenario?
You can use Chrome DevTools Protocol. Try with below steps:
- Add path of Chrome executable to environment variable PATH.
Launch Chrome with custom flag, and open a port for remote debugging
chrome.exe --remote-debugging-port=1111 --user-data-dir="C:\selenum\AutomationProfile"
You can now play with the browser manually, navigate to as many pages, perform actions and once you need your automation code to take charge, you may run your automation script.
You just need to modify your Selenium script to make Selenium connect to that opened browser with below code.
System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe"); chromeOptions options = new ChromeOptions(); options.setExperimentalOption("debuggerAddress", "127.0.0.1:1111"); WebDriver driver = new ChromeDriver(options);`