【发布时间】:2021-01-30 20:32:30
【问题描述】:
我正在尝试使用 selenium 在 python 中创建一个脚本,该脚本将打开一个网站并单击一个按钮。 问题是我必须先关闭 chrome,然后才能运行脚本,否则我会收到此错误:
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
有没有办法解决这个问题,所以我不必在每次运行脚本之前关闭 chrome?
我的代码:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:/Users/username/AppData/Local/Google/Chrome/User Data")
driver = webdriver.Chrome(executable_path=r'C:\Program Files (x86)\chromedriver.exe', chrome_options=options)
driver.get("https://disboard.org/sv/dashboard/servers")
link = driver.find_element_by_link_text("bump")
link.click()
【问题讨论】:
-
只要您使用 Chrome 配置文件,您就必须关闭(某些)Chrome 浏览器。这可以是您的默认 chrome 浏览器,也可以是您可以制作的特定于网络抓取的浏览器。如果您不想关闭任何浏览器,我建议您使用 cookie 而不是用户数据。
标签: python selenium google-chrome selenium-chromedriver chrome-profile