Categories
Python Answers

How to make Python Selenium interact with an existing browser session?

Spread the love

Sometimes, we want to make Python Selenium interact with an existing browser session.

In this article, we’ll look at how to make Python Selenium interact with an existing browser session.

How to make Python Selenium interact with an existing browser session?

To make Python Selenium interact with an existing browser session, we open the driver and the connect to the session with the given session ID.

For instance, we write

driver = webdriver.Firefox()

url = driver.command_executor._url 
session_id = driver.session_id  

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

driver.get("http://www.example.com")

to open the driver with

driver = webdriver.Firefox()

We get the ID of the existing session with

url = driver.command_executor._url 
session_id = driver.session_id  

Then we close the driver and connect to it with the session with

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

Conclusion

To make Python Selenium interact with an existing browser session, we open the driver and the connect to the session with the given session ID.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

One reply on “How to make Python Selenium interact with an existing browser session?”

Hi, Which version did you trai it? I have selenium==4.3.0 . It open the existing brower. But will not open the page or interact with it.

best,

from importlib.resources import path
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service

#driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver = webdriver.Chrome(service=Service(r”C:\Program Files\Google\Chrome\Application\chrome.exe”))

url = driver.command_executor._url
session_id = driver.session_id
print(session_id)

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

driver.get(“http://www.python.org”)

Leave a Reply

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