在Windows上使用Selenium和ChromeDriver:无法创建锁定文件!错误代码:5

问题描述 投票:0回答:2

我的脚本运行良好,但突然出现 chrome 驱动程序错误。似乎某些文件被锁定,但我无法找到。

这是我收到的错误消息:

  d = webdriver.Chrome(r'C:\Users\Rahul\Downloads\chromedriver_win32\chromedriver.exe')
[0306/064432.916:ERROR:process_singleton_win.cc(420)] Lock file can not be created! Error code: 5
[0306/064432.916:ERROR:chrome_browser_main.cc(1424)] Failed to create a ProcessSingleton for your profile directory. This means that running multiple instances would start multiple browser processes rather than opening a new window in the existing process. Aborting now to avoid profile corruption.

任何帮助将不胜感激。

python-3.x google-chrome selenium-webdriver selenium-chromedriver
2个回答
0
投票

这个错误信息...

[0306/064432.916:ERROR:process_singleton_win.cc(420)] Lock file can not be created! Error code: 5
[0306/064432.916:ERROR:chrome_browser_main.cc(1424)] Failed to create a ProcessSingleton for your profile directory. This means that running multiple instances would start multiple browser processes rather than opening a new window in the existing process. Aborting now to avoid profile corruption.

...意味着可能有一些 Zombie ChromeDriver 实例锁定了当前 ChromeDriver 需要使用的配置文件。在执行过程中强行停止 Selenium 往往会留下僵尸进程和相关的进程锁。


解决方案

您需要手动浏览到指定的配置文件目录并删除名为

lockfile
的文件。如果操作系统抱怨该文件正在使用中,请通过杀死所有 实例来关闭它们。


0
投票

从硒导入网络驱动程序

从 selenium.webdriver.chrome.service 导入服务

从 selenium.webdriver.chrome.options 导入选项

导入 chromedriver_autoinstaller

导入临时文件

设置 Chrome 选项

chrome_options = 选项()

chrome_options.add_argument('--disable-gpu')

chrome_options.add_argument('--无沙箱')

chrome_options.add_argument('--disable-dev-shm-usage')

chrome_options.add_argument('--headless') # 可选,根据您的需要

使用 Chrome 用户数据的临时目录以避免锁定文件问题

temp_user_data_dir = tempfile.mkdtemp()

chrome_options.add_argument(f"--user-data-dir={temp_user_data_dir}")

绕过自动化检测

chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])

chrome_options.add_experimental_option('useAutomationExtension', False)

设置正确的 Chrome 二进制路径

chrome_options.binary_location = r"C:\Program Files\Google\Chrome\Applicatio

© www.soinside.com 2019 - 2024. All rights reserved.