【问题标题】:How to control two google chrome profiles using selenium python at the same time?如何同时使用 selenium python 控制两个谷歌浏览器配置文件?
【发布时间】:2021-03-07 12:52:43
【问题描述】:

我正在尝试使用两个不同的配置文件来控制两个 google chrome 实例。但是,当我打开第一个配置文件,然后使用不同的配置文件运行第二个 chrome 实例时,我得到一个错误。

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)

错误日志:

  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)

任何想法如何让它工作?

【问题讨论】:

    标签: python selenium google-chrome


    【解决方案1】:

    我相信你可以在不需要网格的情况下做到这一点。尝试将以下参数添加到 ChromeOptions:

    --disable-dev-shm-usage
    

    【讨论】:

    • 这没有帮助。不幸的是,什么都没有改变。第一个 Chrome 实例崩溃。
    【解决方案2】:

    Google 在下面的文章中声明,如果您同时登录了多个 Google 帐户,Google 可能无法判断您登录的是哪个帐户。我认为解决方案是在登录下一个帐户之前注销一个帐户,或者使用某种方法在两个会话之间保持 cookie 分开。这篇 Google 文章可能会对您有所帮助:

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

    【讨论】:

      【解决方案3】:

      Selenium Grid 将通过并行运行测试来帮助您扩展。只需使用以下命令设置集线器和节点:

      对于中心

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

      对于节点

      java -jar selenium-server-standalone-3.14.0.jar -role node  -hub http://localhost:4444/grid/register
      

      【讨论】:

      • 为什么selenium-server-standalone-2.45.0.jar现在快3岁了,selenium-server-standalone-4.x.jar即将发布?
      • @DebanjanB:是的,你是对的。我已经更新了我的答案。很抱歉造成混乱。
      • 对不起,我在 python 中使用 selenium。
      【解决方案4】:

      这可能与您指定个人资料目录的方式有关。

      以下代码对我有用(似乎与我是否登录没有太大关系)。

      • 假设 chrome/user/data 中的 n > 0 个配置文件文件夹保存为 'Profile1'、'Profile2'、...、'Profilen'

      代码

      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')
      

      例如可以使用以下方式激活浏览器窗口:

      wd[i][0].activate()
      

      或者:

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

      还可以评估 jQuery(例如新标签):

      ev[i]('open()')
      

      当然,您也可以使用 thread_all() 方法在所有浏览器中异步执行此操作。

      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 将是“n”个 chrome 驱动程序(每个独立的浏览器/配置文件一个)

      if __name__ == '__main__':
         vars_glob()
         thread_all(ps,new_profile, workers = n+1, chunk = n+1)
      

      【讨论】:

      猜你喜欢
      • 2021-10-16
      • 1970-01-01
      • 2020-03-16
      • 2011-03-12
      • 1970-01-01
      • 2023-01-13
      • 2014-09-22
      • 1970-01-01
      • 2010-09-07
      相关资源
      最近更新 更多