Home > OS >  Reconnecting To Preexisting Browser In Selenium Causes "No connection could be made because the
Reconnecting To Preexisting Browser In Selenium Causes "No connection could be made because the

Time:12-23

In 2020, I could create a browser, prevent it from disappearing at the end of execution with a strategically raised error, and reconnect to the browser using the method described at https://qxf2.com/blog/reuse-existing-selenium-browser-session/.

Upon revisiting my code this year, for reasons unknown, I can no longer prevent the browser disappearing via strategic error, and switched to the "detach" method described at https://stackoverflow.com/a/51865955

This new approach kicks the following error:

urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=61117): Max retries exceeded with url: /session (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x034689E8>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))

Any help would be greatly appreciated!

I run the code in two steps:

  1. Create persistent browser
from selenium import webdriver
import UtilityFunctions
from selenium.webdriver.chrome.options import Options

# STEP 1, CREATE PERSISTENT BROWSER
executable_path = UtilityFunctions.get_pycharm_root_folder()   "\\Selenium Drivers\\chromedriver.exe"
# 2022/12/20 15:58
# Python selenium keep browser open
# https://stackoverflow.com/a/51865955
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
# 2020/08/24 13:43
# How to reuse existing Selenium browser session
# https://qxf2.com/blog/reuse-existing-selenium-browser-session/
driver = webdriver.Chrome(executable_path, chrome_options=chrome_options)
driver.get("http://www.yahoo.com")
session_url = driver.command_executor._url
session_id = driver.session_id
print(session_url)  # Copy this for STEP 2
print(session_id)  # Copy this for STEP 2
  1. Reconnect to browser created in step 1
from selenium import webdriver
import UtilityFunctions
from selenium.webdriver.chrome.options import Options

# STEP 2, RECONNECT TO BROWSER CREATED IN STEP 1
session_id = "f173a51638c3aca3fd56a95b4df303f1"  # Copied and pasted from output in STEP 1
session_url = "http://localhost:61117"  # Copied and pasted from output in STEP 1
driver = webdriver.Remote(command_executor=session_url, desired_capabilities={})
driver.session_id = session_id
driver.get("http://www.google.com")

CodePudding user response:

You can try the below steps:

  1. First, you need to run the below command in the command prompt:

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222

Before executing the above command, close all the chrome browser windows. After executing the command, the chrome browser will open.

  1. Then you need to execute the below python code:

     from selenium import webdriver
     from selenium.webdriver.chrome.service import Service
     from webdriver_manager.chrome import ChromeDriverManager
     from selenium.webdriver.chrome.options import Options
    
     options = Options()
     options.add_experimental_option("debuggerAddress", "localhost:9222")
     driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options = options)
     driver.get(URL)
     <continue with your code...>
    

You should not close the browser window.

CodePudding user response:

just providing a small update on @abhiSaran's answer. you can also programmatically open the browser in python with the following:

chrome_options = Options()
chrome_options.add_argument("--remote-debugging-port=9222")
chrome_options.add_experimental_option("detach",True)
driver = webdriver.Chrome( options=chrome_options)

and reconnect with:

chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress","127.0.0.1:9222")
driver = webdriver.Chrome(options=chrome_options)
  • Related