13

What is the Selenium equivalent to attaching an existing browser in Watir?

brw = Watir::IE.attach(:title, "Google")
Patrice Gahide
  • 3,644
  • 1
  • 27
  • 37
ram
  • 447
  • 2
  • 5
  • 12
  • 3
    Exact duplicate of [Can Selenium interact with an existing browser session?](http://stackoverflow.com/questions/8344776/can-selenium-interact-with-an-existing-browser-session) – Dan Dascalescu Aug 06 '15 at 04:02

2 Answers2

8

This is a duplicate answer **Reconnect to a driver in python selenium ** This is applicable on all drivers. 1. open a driver

driver = webdriver.Firefox()

2. extract to session_id and _url from driver object.

url = driver.command_executor._url       #"http://127.0.0.1:60622/hub"
session_id = driver.session_id            #'4e167f26-dc1d-4f51-a207-f761eaf73c31'

3. Use these two parameter to connect to your driver.

driver = webdriver.Remote(command_executor=url,desired_capabilities={})
driver.session_id = session_id

And you are connected to your driver again.

driver.get("http://www.mrsmart.in")
Manoj Sahu
  • 2,774
  • 20
  • 18
  • 1
    Good, but one problem is, the second test process will start a new browser. Could that be avoid? – Jcyrss Jan 11 '17 at 03:21
  • Do not initiate a new webdriver but for testing purpose it is good practice to initiate a new browser for each test case. – Manoj Sahu Jan 11 '17 at 06:45
  • 2
    If I'm not wrong , actually the new browser (with new session created ) started is useless. we will use the existing old session to conduct the task. – Jcyrss May 02 '17 at 14:30
  • 3
    Got error "urllib2.URLError: " When I try to reconnect chromedriver, Is there a way to solve this? Thanks! – Til Oct 05 '17 at 15:30
  • driver.session_id is not valid syntax! (session_id cannot be resolved or is not a field) – Steve Staple Jan 12 '18 at 16:12
  • are you using python api for selenium web driver. – Manoj Sahu Jan 15 '18 at 05:57
  • This is not the existing browser we meant, we mean a browser opened by double click the desktop CHROME ICON! – yuzhen Aug 11 '19 at 10:11
  • @Jcyrss im not sure if it is possible to avoid opening a second browser, but what u can do is before setting the old session id, close the newly opened browser with driver.close() – illgoforit Sep 22 '19 at 11:02
5
  1. Run a Webdriver first

    driver = new FirefoxDriver();

  2. Now run a RemoteWebdriver

    DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    driver = new RemoteWebDriver(new URL("http://localhost:7055/hub"),capabilities);

The RemoteWebdriver will attach to the first browser window running Webdriver and it will not create a new window.


Note: Run Webdriver (1) and RemoteWebdriver (2) in separate programs one-by-one).

Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
bpk
  • 313
  • 2
  • 6
  • 20
  • 1
    Correct! Here's how to attach to a running server using `selenium-webdriverjs`: `var driver = new webdriver.Builder() .withCapabilities(webdriver.Capabilities.chrome()) .usingServer('http://localhost:9515') .build(); `. Run this after you've launched .e.g. `chromedriver --port=9515`. – Dan Dascalescu Aug 06 '15 at 03:57
  • 1
    @DanDascalescu, I was not aware that we could do this on Chrome as well..!! Do you mind explaining me how to launch a chromedriver on port=9515? As Firefox default port is 7055 it works fine for me but how about Chrome? – bpk Aug 10 '15 at 10:51
  • Just run `chromedriver --port=9515` after [downloading ChromeDriver](http://chromedriver.storage.googleapis.com/index.html)... if I understand the question correctly? – Dan Dascalescu Aug 10 '15 at 17:10
  • @DanDascalescu I'm just wondering what code/syntax you use to launch it on 9515 port? driver = new ChromeDriver(); --> will this do? – bpk Aug 11 '15 at 19:42
  • 1
    @DanDascalescu I managed to run chromedriver --port=9515 and after that every time when I run the above webdriver code it opens a new chrome browser and it is not attaching to the first one! – bpk Aug 18 '15 at 14:52