Nitin_k29
Nitin_k29

Reputation: 351

Connect to an already running instance of chrome using selenium in python

I am facing an issue with chrome while launching it with extensions using selenium. I have logged an issue https://code.google.com/p/chromedriver/issues/detail?id=508

For a workaround I am planing to launch chrome than enable required extension, after this connect to it using selenium.

But I am unable to so so. Can anyone help in this matter as d=webdriver.Chrome() always launches a new chrome instance. I want to connect to an already running instance of chrome.

Upvotes: 5

Views: 12929

Answers (4)

barlop
barlop

Reputation: 13743

You can, it's covered here

what does "add_experimental_option("debuggerAddress", "localhost:8989")" actually do when added to Options() on Python? Selenium related

So e.g.

If you do

(adjust the command for your needs, your chrone path.. and the user profile you want to use). chrome://version can show you the chrome user profile folder for whatever chrome user.

C:\Users\User>"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"  --remote-debugging-port=8989 --user-data-dir="C:\Users\User\App
Data\Local\Google\Chrome\User Data\Profile 3"

C:\Users\User>

Then in your python program

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

opt = Options()
opt.add_experimental_option("debuggerAddress", "localhost:8989")
selclient = webdriver.Chrome(options=opt)
selclient.get("http://www.dogpile.com")

That python code will not launch a new chrome window. It will connect to the already existing chrome instance that you started from eg the command line.

Upvotes: 0

Manoj Sahu
Manoj Sahu

Reputation: 2932

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

    driver = webdriver.Firefox()
  1. 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
  1. And you are connected to your driver again.

    driver.get("http://www.mrsmart.in")

Upvotes: 8

MetaMemoryT
MetaMemoryT

Reputation: 174

Now it is,

  1. Fire up the java server
  2. Hit localhost:4444/wd/hub
  3. Create a session for the appropriate browser
  4. Create a remote webdriver client using the given session ID (syntax may vary depending on language bindings you are using)

reference: github issue

Upvotes: 3

Arran
Arran

Reputation: 25056

This is simply not possible in Selenium, in any browser and in any programming language.

https://code.google.com/p/selenium/issues/detail?id=18

Upvotes: 6

Related Questions