0

I have a script in Python that uses Selenium to automatically navigate the web.

I'm upgrading my script to make it work even if Chrome has already been opened, attaching the driver to the current session.

The code I'm using, which works in part, is:

def attach_to_session(executor_url, session_id):
    original_execute = WebDriver.execute
    def new_command_execute(self, command, params=None):
        if command == "newSession":
            # Mock the response
            return {'success': 0, 'value': None, 'sessionId': session_id}
        else:
            return original_execute(self, command, params)
    # Patch the function before creating the driver object
    WebDriver.execute = new_command_execute
    driver = webdriver.Remote(command_executor=executor_url, desired_capabilities={})
    driver.session_id = session_id
    # Replace the patched function with original function
    WebDriver.execute = original_execute
    return driver

service = Service('C:\\Users\\nicoc\\PycharmProjects\\WriteAI\\chromedriver.exe')

driver = webdriver.Chrome(service=service)

executor_url = driver.command_executor._url
session_id = driver.session_id
driver = attach_to_session(executor_url, session_id)

It works in part because, it correctly open a new browser window and start navigating according to my code, with an already existing Chrome session open, but it won't use the Chrome profile the already opened session is using.

Also, an even better solution would be to open the driver with a new tab on the already opened Chrome window, instead of opening a new Chrome Window when launching the driver.

I can still manage that the driver opens a new Chrome window, as soon as it uses the specific profile that it's in use on the active Chrome session.

I have already tried to add the option arguments with

driver = webdriver.Chrome(service=service, options=chrome_options)
chrome_options = Options()
chrome_options.add_argument\
    (r"--user-data-dir=C:\\Users\\nicoc\\AppData\\Local\\Google\\Chrome\\User Data")
chrome_options.add_argument(r'--profile-directory=Default')

but I got an error saying the profile is already in use

EDIT using the driver.get("https://www.youtube.com/") while a chrome instance is open returns the error below:

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir

if I remove the option

chrome_options.add_argument(r"--user-data-dir=C:\\Users\\nicoc\\AppData\\Local\\Google\\Chrome\\User Data")

it opens a new Chrome window but, again, with no profile loaded

NictheDEV
  • 147
  • 12
  • `but I got an error saying the profile is already in use` - Close the Chrome browser that has been opened and then run the program – pmadhu Oct 06 '21 at 10:30
  • @pmadhu you haven't read the question I guess... The point is to attach the driver to the existing session with that user. As I mention, everything works if I close the browser; I want to launch my script with the browser already opened. – NictheDEV Oct 06 '21 at 10:35
  • I did read the the question, but focused on `profile is already in use` part. I am not able to understand. Do you want to open new tabs during automation or open a new URL in the same tab? – pmadhu Oct 06 '21 at 10:49
  • @pmadhu opening a new tab would be best but using an existing tab would also be ok. I just want to attach the driver to the existing Chrome browser which is already open. PS: it's clearly stated in my question: "an even better solution would be to open the driver with a new tab on the already opened Chrome window" – NictheDEV Oct 06 '21 at 11:10
  • I have tried to mention the ways to open tabs in the browser. But I am not sure if that is going to help you. – pmadhu Oct 06 '21 at 11:24

1 Answers1

0

To open a new tab you can make use of JavaScript:

driver.get("https://www.youtube.com/")

# This opens a new tab and loads another URL
driver.execute_script("window.open('https://www.linkedin.com/');")
driver.get("https://www.youtube.com/")

# This opens a new URL in the same tab
driver.get("https://www.linkedin.com/")

To open a browser tab in the existing tab, you can make use of webbrowser in python. But I dont think we can automate through that.

import webbrowser

webbrowser.open_new_tab("https://www.linkedin.com/")

Links you might want to refer: Open web in new tab Selenium + Python https://docs.python.org/3/library/webbrowser.html

pmadhu
  • 3,373
  • 2
  • 11
  • 23
  • this solution does not attach to an already existing instance of Chrome. This just opens the driver and opens a new tab. With a Chome instance already open the code returns an error (I already tested). I'm glad and grateful you're trying to help me but, please, try to understand my question first. – NictheDEV Oct 06 '21 at 11:39
  • And what is that error? – pmadhu Oct 06 '21 at 11:43
  • user already in use. Updated my question with the error – NictheDEV Oct 06 '21 at 11:56