0

I am trying to control two google chrome instances using two different profiles. But when I open first profile and then run second chrome instance with different one I get an errors.

from selenium import webdriver


def launch(login, password):
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument(r'--user-data-dir=/home/marek/.config/google-chrome')
    chrome_options.add_argument(r'--profile-directory=Profile 3')
    second_options = webdriver.ChromeOptions()
    second_options.add_argument(r'--user-data-dir=/home/marek/.config/google-chrome')
    second_options.add_argument(r'--profile-directory=Profile 4')
    driver = webdriver.Chrome(options = chrome_options)
    second_driver = webdriver.Chrome(options = second_options)
    driver.get('http://google.com')
    second_driver.get('http://google.com')

if __name__ == '__main__':
    login = 'xxx'
    password = 'xxx'
    launch(login,password)

Error logs :

  File "auto.py", line 19, in <module>
    launch(login,password)
  File "auto.py", line 11, in launch_draw
    driver = webdriver.Chrome(options = chrome_options)
  File "/usr/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/usr/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/lib/python3.7/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: Chrome failed to start: exited abnormally
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /opt/google/chrome/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
  (Driver info: chromedriver=72.0.3626.69 (3c16f8a135abc0d4da2dff33804db79b849a7c38),platform=Linux 4.19.23-1-MANJARO x86_64)

Any ideas how to get this working?

mareks
  • 124
  • 8

5 Answers5

0

Selenium Grid will help you scale by running tests in parallel. Just setup a hub and node with the following commands:

For the hub

java -jar selenium-server-standalone-3.14.0.jar -role hub

and for the node

java -jar selenium-server-standalone-3.14.0.jar -role node  -hub http://localhost:4444/grid/register
KunduK
  • 32,888
  • 5
  • 17
  • 41
0

Google states in the article below that is you are logged into more than one Google account at once, Google may not be able to tell which account you are logged into. I would think the solution would be to log out of one account before logging into the next, or use some method of keeping the cookies separate between the two sessions. This Google article may help you:

https://support.google.com/accounts/answer/1721977?co=GENIE.Platform%3DDesktop&hl=en

Mike C.
  • 1,761
  • 2
  • 22
  • 46
0

I believe you can do this without needing the grid. Try adding the following argument to ChromeOptions:

--disable-dev-shm-usage
C. Peck
  • 3,641
  • 3
  • 19
  • 36
0

It might have something to do with how you are specifying your profile directory.

The following code works for me (doesn't seem to have much to do with whether I'm logged in or not).

  • Assumes n > 0 profile folders in chrome/user/data saved as 'Profile1', 'Profile2', ..., 'Profilen'

Code

from selenium import webdriver
from selenium.webdriver.chrome.options import DesiredCapabilities
from concurrent.futures import ThreadPoolExecutor
import os, pyautogui, easygui

def new_profile(i):
    global profiles, parent, ev

    profiles[i] = path_profile + str(i)
    o = webdriver.ChromeOptions()
    o.add_argument('--user-data-dir=' + profiles[i])
    d[i] = webdriver.Chrome(path_exec, options=o)
    parent[i] = d[i].current_window_handle
    [ev[i]] = [d[i].execute_script]
    nu = 'x' if i < 10 else 'y'
    try:
        d[i].get('https://' + nu + str(i))
    except:
        pass
    wd[i] = gwwt('x' + i) if i < 10 else gwwt('y' + i)
    d[i].get('google.co.uk')

E.g. could activate ith browser window using:

wd[i][0].activate()

alternatively:

wd[i][0].minimize()
wd[i][0].restore

Could also evaluate jQuery (e.g. new tab):

ev[i]('open()')

Of course, you can also do this across all browsers asynchronously using thread_all() method.

def thread_all(ps, fn, parm='', actions=[], workers=6, chunk=1):
    print(f'thread_all({ps}, {fn}, {parm}, {actions}')
    if parm == '':
        with ThreadPoolExecutor(max_workers=max(1, workers)) as executor:
            return executor.map(fn, ps, timeout=90, chunksize=max(1, chunk))
    else:
        with ThreadPoolExecutor(max_workers=max(1, workers)) as executor:
            return executor.map(fn, ps, itertools.repeat(parm, L), timeout=90, chunksize=max(1, chunk))


def vars_glob():
    global path_profile, path_core, path_exec
    global d, profiles, parent, ev, wd, n, ps
    global gwwt
    resp = easygui.multenterbox('Enter preferences', 'Inputs', ['Number of browsers'],[4])
    try:
        n = int(resp[0])
    except:
        print('enter a digit! run code again please :(')
        quit()
        [gwwt] = [pyautogui.getWindowsWithTitle]
        profiles, d, parent, ev, wd = [''] * n, [''] * n, [''] * n, [''] * n, [['']] * n
        ps = list(range(n))
        path_core = os.path.expanduser("~")
        path_profile = os.path.join(path_core, "AppData", "Local", "Google", "Chrome", "User", "Data", "Profile")
        path_exec = os.path.join(path_core, '< rest of path >', 'chromedriver.exe')

d will be 'n' chrome-drivers (one for each independent browser/profile)

if __name__ == '__main__':
   vars_glob()
   thread_all(ps,new_profile, workers = n+1, chunk = n+1)
JB-007
  • 2,156
  • 1
  • 6
  • 22
  • https://stackoverflow.com/questions/38411728/cannot-open-two-google-chrome-instances-with-different-profiles-using-selenium-w – JB-007 Jun 01 '20 at 17:02
  • Updated previous answer to allow for multi-threading... Caveats: untested (some jiggery-pokery might be required). – JB-007 Mar 07 '21 at 04:53
0

You can't control two profiles with the same selenium webdriver. Try to separate them by running one profile at a time. The solution while using undetected_chromedriver. The same applies to the normal selenium webdriver

 import undetected_chromedriver as uc

chrome_profile_path='C:\\Users\\your_name\\AppData\\Local\\Google\\Chrome\\User Data'
    options = uc.ChromeOptions()
    options.add_argument("window-size=1200,700")
    options.add_argument(f"user-data-dir={chrome_profile_path}")
    options.add_argument('profile-directory=Profile 1')
    driver = uc.Chrome(options=options)
Maina
  • 11
  • 2