如何同时使用Selenium Python控制两个Google Chrome配置文件?

时间:2019-03-03 15:08:06

标签: python selenium google-chrome

我正在尝试使用两个不同的配置文件控制两个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)

有什么想法可以使它正常工作吗?

4 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

Google在下面的文章中指出,您一次登录了多个Google帐户,但Google可能无法告知您登录的帐户。我认为解决方案是先注销一个帐户,然后再登录下一个帐户,或者使用某种方法将cookie保持在两个会话之间。 Google的这篇文章可能对您有帮助:

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

答案 2 :(得分:0)

我相信您可以在不需要网格的情况下执行此操作。尝试将以下参数添加到ChromeOptions:

--disable-dev-shm-usage

答案 3 :(得分:0)

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

以下代码对我有用(无论我是否登录,似乎都没有多大关系。)

指导:

  1. 即使不需要,我也包含了DesiredCapabilities(以防万一您想使用它)
  2. 替换为您的用户名!
  3. 根据需要替换
  4. 为2个浏览器指定n=2,为3个浏览器指定n=3,依此类推...
  5. 将创建用户个人资料Profile PyX,其中X= 1, 2, 3... etc.(取决于您同时启动的浏览器数量)
  6. 如果您在浏览器启动后向这些配置文件添加chrome扩展名,它们将在您下次运行代码时重新出现/自动加载...
from selenium import webdriver
from selenium.webdriver.chrome.options import DesiredCapabilities
'''if you remove this, do same for 'v_caps.append(DesiredCapabilities.Chrome.copy())' 
below'''
def launch(n):
    global v_drivers
    v_profiles = []; v_options = []; v_chromedrivers = []; v_caps = []
    #modify profile path and chromedrv as req/appropriately:
    profile_path = "C:/Users/<user>/AppData/Local/Google/Chrome/User Data/Profile Py"
    chromedrv_path = "C:/Users/<username>/<rest of path>/chromedriver.exe"
    for i in range(n):
        v_profiles.append(profile_path + str(i) + "/")
        v_options.append(webdriver.ChromeOptions())
        v_caps.append(DesiredCapabilities.CHROME.copy())
        v_chromedrivers.append(chromedrv_path)
        v_options[i].add_argument('--user-data-dir=' + v_profiles[i])
        #can remove as required - see note (a) above
        v_caps[i]["pageLoadStrategy"] = "eager"
        v_drivers.append(webdriver.Chrome(desired_capabilities= v_caps[i],
        executable_path=v_chromedrivers[i], options=v_options[i]))

        v_drivers[i].get('http://google.com')


v_drivers=[]
if __name__ == '__main__':
    n=2 #number of browsers you want to open
    launch(n)