【问题标题】:Why does opt.add_argument('--user-data-dir='+r'path') work but opt.add_argument('--user-data-dir='+fr'"{path}"') doesn't as an option to Selenium?为什么 opt.add_argument('--user-data-dir='+r'path') 工作,但 opt.add_argument('--user-data-dir='+fr'"{path}"') 没有' t 作为 Selenium 的一个选项?
【发布时间】:2023-03-12 04:49:01
【问题描述】:

在尝试使用--user-data-dir--profile-directory 的用户在Python 3.9.7 上部署chrome driver 时,我意识到了一些非常奇怪的事情,见下文:

如果编译如下代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

opt = Options() #the variable that will store the selenium options

opt.add_argument('--user-data-dir='+r'C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data') #Add the user data path as an argument in selenium Options
opt.add_argument('--profile-directory=Default') #Add the profile directory as an argument in selenium Options
s = Service('C:/Users/ResetStoreX/AppData/Local/Programs/Python/Python39/Scripts/chromedriver.exe')

driver = webdriver.Chrome(service=s, options=opt) 
driver.get('https://opensea.io/login?referrer=%2Faccount')

你使用对应的--user-data-dir--profile-directory成功得到一个chrome驱动实例:

现在,cmd上使用以下代码杀死所有chrome驱动程序实例:

taskkill /F /IM chromedriver.exe

然后编译这个其他代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

opt = Options() #the variable that will store the selenium options

path = input('Introduce YOUR profile path:')

opt.add_argument('--user-data-dir='+fr'"{path}"') #Add the user data path as an argument in selenium Options
opt.add_argument('--profile-directory=Default') #Add the profile directory as an argument in selenium Options
s = Service('C:/Users/ResetStoreX/AppData/Local/Programs/Python/Python39/Scripts/chromedriver.exe')

driver = webdriver.Chrome(service=s, options=opt) 
driver.get('https://opensea.io/login?referrer=%2Faccount')

最后输入:C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data 作为输入

您收到此错误

WebDriverException:未知错误:无法删除旧的 devtools 端口 文件。也许给定的用户数据目录位于 “C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data” 仍附加到正在运行的 Chrome 或 Chromium 进程

为什么会这样?

opt.add_argument('--user-data-dir='+fr'"{path}"') 不是传递此用户数据路径的有效方式:

path = C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data?

【问题讨论】:

  • 给 add_arguments 的 最终字符串值 之间的实际区别是什么?没有其他事情重要或与直接的 A-B 相关。
  • 不同之处在于,在提供 chrome 浏览器的配置文件路径后,第二个代码将用于创建一个程序,该程序将为用户执行一些自动化工作。 @user2864740

标签: python-3.x debugging selenium-webdriver path selenium4


【解决方案1】:

我想通了,我用opt.add_argument('--user-data-dir='+fr'"{path}"')创建了一个语法错误,所以我把它改成了opt.add_argument('--user-data-dir='+fr'{path}'),改进后的代码如下:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

opt = Options() #the variable that will store the selenium options

path = input('Introduce YOUR profile path:')

opt.add_argument('--user-data-dir='+fr'{path}') #Add the user data path as an argument in selenium Options
opt.add_argument('--profile-directory=Default') #Add the profile directory as an argument in selenium Options
s = Service('C:/Users/ResetStoreX/AppData/Local/Programs/Python/Python39/Scripts/chromedriver.exe')

driver = webdriver.Chrome(service=s, options=opt) 
driver.get('https://opensea.io/login?referrer=%2Faccount')

编译此代码后,程序将运行而不会抛出任何错误,并获得与本文中显示的第一个代码相同的结果。

【讨论】:

    猜你喜欢
    • 2022-01-20
    • 2017-06-15
    • 2021-07-06
    • 2019-01-28
    • 2018-11-11
    • 2015-08-23
    • 2023-01-20
    相关资源
    最近更新 更多