如何正确使用Selenium Chrome Webdriver和Python中的自定义配置文件?

3
options_ = webdriver.ChromeOptions()
options_.add_argument("user-data-dir=C:\\Users\\Anton\\\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
driver = webdriver.Chrome(executable_path='C:\chromedriver\chromedriver.exe', chrome_options=options_)

我正在尝试使用当前的Chrome配置文件,以避免在我的自动化项目中遇到验证码。

然而,Selenium拒绝加载除了临时配置文件之外的任何其他配置文件。我的语法有误吗?'chrome_options = options_' 给出了弃用消息,但将其更改为'options=options_' 对结果没有任何影响。

编辑: Amit YR的评论解决了我的初始问题,现在Selenium用正确的用户配置文件打开Chrome。

但是,现在出现了一个新问题。 浏览器打开后,脚本停止并显示以下错误:

Traceback (most recent call last):
  File "C:\Users\Anton\Documents\pytho.py", line 24, in <module>
    driver = webdriver.Chrome(executable_path='C:\chromedriver\chromedriver.exe', options=options_)
  File "C:\Users\Anton\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "C:\Users\Anton\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Users\Anton\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Users\Anton\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Anton\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
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
1个回答

2

从"user-data-dir=C:\Users\Anton\\AppData\Local\Google\Chrome\User Data\Default"中删除默认值,因为chrome会将默认值添加到配置文件路径中。

相反,您应该使用以下内容:

options_.add_argument("user-data-dir=C:/Users/Anton/AppData/Local/Google/Chrome/User Data")

这个修复后又出现了另一个问题。已更新原帖。 - antonb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接