InvalidArgumentException:消息:无效参数:使用ChromeDriver Selenium启动Chrome时,用户数据目录已在使用中错误 [英] InvalidArgumentException: Message: invalid argument: user data directory is already in use error while initiating Chrome with ChromeDriver Selenium

查看:2292
本文介绍了InvalidArgumentException:消息:无效参数:使用ChromeDriver Selenium启动Chrome时,用户数据目录已在使用中错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用默认用户使用selenium python库打开网页,该脚本使用默认用户至关重要,但是如果我的chrome浏览器已经打开,则脚本崩溃并给我这个错误:

I am trying to open a web page using the selenium python library with my default user, it is critical that the script uses the default user but if my chrome browser is already open the script crashes and gives me this error:

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

我尝试了这里给出的所有解决方案:

I have tried all the solutions given here :

Selenium chromedriver无法启动URL如果另一个Chrome实例打开了

Selenium无法打开在新标签(Python& Chrome)中添加新网址

并阅读到较旧的chromedriver版本中存在一个错误,但已在chrome 74(即时通讯使用)中修复了该错误: https://github.com/SeleniumHQ/docker-selenium/issues/741

and read that there was a bug in older chromedriver versions but it was fixed in chrome 74 (which im using ) : https://github.com/SeleniumHQ/docker-selenium/issues/741

from selenium import webdriver
import time
from getpass import getuser

def run():
    # Chrome driver path
    chromedriver = r'C:\Users\user1\Downloads\chromedriver_win32\chromedriver_new.exe'

    # Get chrome webdriver options and set open the browser as headless
    chrome_options = webdriver.ChromeOptions()
    #chrome_options.add_argument("--headless")

    # Fix for selenium Issue 2907
    #chrome_options.add_argument('--log-level=3')
    #chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])

    # Load current user default profile
    current_user = getuser()
    chrome_options.add_argument(
        r"--user-data-dir=C:\Users\{}\AppData\Local\Google\Chrome\User Data".format(current_user))

    # didable "Chrome is being controled by an automated test software"
    chrome_options.add_argument('disable-infobars')

    # get Chrome to stay open
    chrome_options.add_experimental_option("detach", True)

    # open browser with options and driver
    driver = webdriver.Chrome(options=chrome_options, executable_path=chromedriver)
    driver.get(r'https://www.youtube.com/watch?v=dQw4w9WgXcQ')



if __name__ == '__main__':
    run()

如果我在没有Chrome浏览器的情况下运行它,即使没有崩溃,也可以正常打开

if i run it without a chrome browser open its fine if not it crashes

推荐答案

我也想使用默认的Chrome配置文件运行Selenium,但是遇到了同样的问题.我通过将UserData文件夹复制到另一个位置来解决此问题,然后使用了新位置.这是我完整的代码:

I also wanted to run Selenium using my default Chrome profile but I came across the same issue. I solved it by copying my UserData folder to another location then I used the new location. Here is my complete code:

from selenium import webdriver
options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=C:\\Users\\myusername\\Desktop\\User Data")
options.add_argument("--profile-directory=Profile 1");
browser = webdriver.Chrome(options=options) 
browser.get('https://www.google.com')

如果您要使用默认的Chrome配置文件,而不是为Selenium创建的特定配置文件,请从代码中删除以下行.

Remove the following line from the code if you would like to use your default Chrome profile not a specific profile you created for Selenium.

options.add_argument("--profile-directory=Profile 1");

这篇关于InvalidArgumentException:消息:无效参数:使用ChromeDriver Selenium启动Chrome时,用户数据目录已在使用中错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆