1

I was following this tutorial (link below), but an error is happening that I don't know how I can solve it.

https://medium.com/@harith.sankalpa/connect-selenium-driver-to-an-existing-chrome-browser-instance-41435b67affd

I am trying to use the browser that is open to perform a search, because it is already logged into the account that I need.

I'm using chrome --remote-debugging-port=1024 to open chrome, after I execute this code below.

I found some solutions, but none solved my problem, either because the solution was in Java and I didn't understand or I didn't know how to rewrite in python.

Code:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

if __name__ == '__main__':
    options = ChromeOptions()
    options.add_argument('start-maximized')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    options.add_argument('--remote-debugging-port=1024')
    options.add_argument('--disable-setuid-sandbox')
    options.add_experimental_option("debuggerAddress", "localhost:1024")
    options.binary_location = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"

    d = DesiredCapabilities.CHROME
    d["loggingPrefs"] = {"browser": "ALL"}

    driver = webdriver.Chrome(
        executable_path=ChromeDriverManager().install(),
        options=options,
        desired_capabilities=d
    )

    driver.get("https://google.com.br")

Error:

Traceback (most recent call last):
  File "C:/Users/danit/Desktop/project/main.py", line 22, in <module>
    desired_capabilities=d
  File "C:\Users\danit\Desktop\project\venv\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "C:\Users\danit\Desktop\project\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Users\danit\Desktop\project\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Users\danit\Desktop\project\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\danit\Desktop\project\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to chrome at localhost:1024
from chrome not reachable

Thanks a lot.

Cava
  • 5,346
  • 4
  • 25
  • 41

1 Answers1

1

The error says chrome is not reachable. Most probably that instance has been deleted

I am showing a simple method to save session data( all cookies will be saved ) and then load selenium to load from that instance.

Look at the following example


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

session = "mySession"
chrome_driver_path = '/home/aahnik/Downloads/apps/chromedriver'
whatsapp_web_url = "https://web.whatsapp.com/"

chrome_options = Options()
chrome_options.add_argument(f'--user-data-dir={session}')

driver = webdriver.Chrome(
    options=chrome_options, executable_path=chrome_driver_path)

driver.get(whatsapp_web_url)

Now execute this code. WhatsApp web will open. Login by scanning the QR Code.

Now close the window, and then terminate the program.

Now will see a folder named mySession in your current user directory.

Execute this code again.

This time you will find that you are already logged into WhatsApp.

Hope this helped.

aahnik
  • 1,661
  • 1
  • 11
  • 29