ChromeDriver 假设 Chrome 在传递配置文件参数时崩溃了 [重复]
如果任何 Chrome 进程正在运行,并且以下代码正在运行,Stack Overflow 将 永远 不加载:
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\RvBVakama\\AppData\\Local\\Google\\Chrome\\User Data")
options.add_argument('--profile-directory=Profile 1')
w = webdriver.Chrome('C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe', options=options)
w.get("https://stackoverflow.com/")
如果任何 Chrome 进程存在,并且以下代码正在运行,它 将在几分钟后加载 Stack Overflow:
w = webdriver.Chrome('C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe')
w.get("https://stackoverflow.com/")
唯一的 区别 是第一个代码块使用配置文件 1 启动 Chrome。
这是第一个代码块的错误日志:
[13768:7324:1216/205746.092:ERROR:cache_util_win.cc(19)] Unable to move the cache: 0
[13768:7324:1216/205746.093:ERROR:cache_util.cc(140)] Unable to move cache folder C:\Users\RvBVakama\AppData\Local\Google\Chrome\User Data\ShaderCache\GPUCache to C:\Users\RvBVakama\AppData\Local\Google\Chrome\User Data\ShaderCache\old_GPUCache_000
[13768:7324:1216/205746.094:ERROR:disk_cache.cc(184)] Unable to create cache
[13768:7324:1216/205746.095:ERROR:shader_disk_cache.cc(622)] Shader Cache Creation failed: -2
Traceback (most recent call last):
File "c:\Users\RvBVakama\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\ptvsd_launcher.py", line 45, in <module>
main(ptvsdArgs)
File "c:\Users\RvBVakama\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\__main__.py", line 265, in main
wait=args.wait)
File "c:\Users\RvBVakama\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\__main__.py", line 256, in handle_args
run_main(addr, name, kind, *extra, **kwargs)
File "c:\Users\RvBVakama\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_local.py", line 52, in run_main
runner(addr, name, kind == 'module', *extra, **kwargs)
File "c:\Users\RvBVakama\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\runner.py", line 32, in run
set_trace=False)
File "c:\Users\RvBVakama\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\pydevd.py", line 1283, in run
return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
File "c:\Users\RvBVakama\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\pydevd.py", line 1290, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "c:\Users\RvBVakama\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\_pydev_imps\_pydev_execfile.py", line 25, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "c:\Users\RvBVakama\Desktop\tet.py", line 9, in <module>
w = webdriver.Chrome('C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe', options=options)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location C:\Program Files (x86)\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
(Driver info: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),platform=Windows NT 6.1.7601 SP1 x86_64)
注意最后一行。
我不确定为什么 chromedriver 认为 Chrome 已崩溃。
好的,解决方法是转到 C:\Users\NAME\AppData\Local\Google\Chrome 并复制您的“用户数据”文件夹并为其指定另一个名称。 然后只需将您的“user-data-dir”参数设置为新文件夹即可。
# set up the chrome options to launch with the AUTO profile
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\RvBVakama\\AppData\\Local\\Google\\Chrome\\User Data AUTO")
options.add_argument('--profile-directory=Profile 1')
# start chrome with the driver
w = webdriver.Chrome('C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe', options=options)
我不确定它是否是相同的用户数据目录,但不同的配置文件会导致冲突。我总是在脚本中使用另一个用户数据目录。
-- 但看到你的情况,我猜是这样。
另外,顺便说一句,为了让生活更轻松,为什么不直接复制到一些较短的目录地址呢? --- 比如说“C:\ChromeUserData”。
如果你不关心旧设置,只需将用户数据目录指定为新位置。Chrome 将为你创建配置文件和其他内容。 (指定的目录必须存在。)
例如:
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=D:\\chromedriver\\UserDataDir") #Path to your chrome user-data/profile
options.add_argument('--disable-infobars') #disable the automation prompt bar
options.add_argument('--lang=en') #Set language to English
w = webdriver.Chrome(executable_path="D:\\chromedriver\\chromedriver.exe", chrome_options=options)
假设 D:\chromedriver\UserDataDir 存在(或者您先创建它) 并且 chromedriver.exe 位于 D:\chromedriver\chromedriver.exe 。
这样,您就不需要再指定配置文件目录了。该脚本将在
D:\\chromedriver\\UserDataDir
中创建并使用默认目录/配置文件,并保存您所做的所有更改。
另一个提示:您可以将 chromedriver.exe 放在 python.exe 的目录中,这样就无需指定 executable_path,出错的可能性也会更小。
为了使其更可靠,您可以将这些 前面 的代码片段放在上面:
import os
uddPath = 'D:\\chromedriver\\UserDataDir'
if not os.path.exists(uddPath):
os.makedirs(uddPath)