我正试图让selenium打开一个用户配置文件,这样我就可以自动化我的谷歌账户中的内容,而无需每次登录。使用下面的代码,我成功地打开了chrome,但它一打开就崩溃了,这是我的代码。(我的操作系统是MacOS Ventura(
我的chromdriver
和我的Google Chrome是同一版本的
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
service = Service("/usr/local/bin/chromedriver")
ch_options = Options() # options
ch_options.add_argument("user-data-dir=/Users/matheus/Library/Application Support/Google/Chrome")
ch_options.add_argument("--profile-directory=Profile 2")
driver = webdriver.Chrome(service=service, options=ch_options)
driver.get("https://gmail.com")
这是故障日志
Traceback (most recent call last):
File "/Users/matheus/dev/work-automations/main.py", line 12, in <module>
driver = webdriver.Chrome(service=service, options=ch_options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/matheus/dev/work-automations/venv/lib/python3.11/site-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
super().__init__(DesiredCapabilities.CHROME['browserName'], "goog",
File "/Users/matheus/dev/work-automations/venv/lib/python3.11/site-packages/selenium/webdriver/chromium/webdriver.py", line 92, in __init__
super().__init__(
File "/Users/matheus/dev/work-automations/venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 272, in __init__
self.start_session(capabilities, browser_profile)
File "/Users/matheus/dev/work-automations/venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 364, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/matheus/dev/work-automations/venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 429, in execute
self.error_handler.check_response(response)
File "/Users/matheus/dev/work-automations/venv/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 243, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited normally.
(chrome not reachable)
(The process started from chrome location /Applications/Google Chrome.app/Contents/MacOS/Google Chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Stacktrace:
0 chromedriver 0x000000010276aa88 chromedriver + 4123272
1 chromedriver 0x00000001026f6778 chromedriver + 3647352
2 chromedriver 0x00000001023b8ac4 chromedriver + 248516
3 chromedriver 0x00000001023dc2a4 chromedriver + 393892
4 chromedriver 0x00000001023d8858 chromedriver + 378968
5 chromedriver 0x00000001024125a0 chromedriver + 615840
6 chromedriver 0x00000001024121d8 chromedriver + 614872
7 chromedriver 0x00000001023e2b10 chromedriver + 420624
8 chromedriver 0x00000001023e3c30 chromedriver + 425008
9 chromedriver 0x000000010273cae4 chromedriver + 3934948
10 chromedriver 0x000000010273ff24 chromedriver + 3948324
11 chromedriver 0x0000000102740508 chromedriver + 3949832
12 chromedriver 0x0000000102746b30 chromedriver + 3975984
13 chromedriver 0x0000000102740b24 chromedriver + 3951396
14 chromedriver 0x000000010271b71c chromedriver + 3798812
15 chromedriver 0x000000010275d2f0 chromedriver + 4068080
16 chromedriver 0x000000010275d444 chromedriver + 4068420
17 chromedriver 0x0000000102771450 chromedriver + 4150352
18 libsystem_pthread.dylib 0x000000018421a06c _pthread_start + 148
19 libsystem_pthread.dylib 0x0000000184214e2c thread_start + 8
Process finished with exit code 1
为概要文件2提供的路径似乎是位于库中的原始路径。
如果您的浏览器同时处于打开状态,则可能会导致冲突。我建议复制原始文件夹并使用它。
如果你不知道如何访问它:
Finder>转到>按选项键(将显示库(>库>应用程序支持>谷歌>铬>配置文件2
所以我修改了你的代码,它应该是这样的:我允许您根据放置个人资料副本的位置更改路径。
from selenium import webdriver
ch_options = webdriver.ChromeOptions()
ch_options.add_argument("--user-data-dir=/Users/matheus/desktop/Selenium Profile/Profile 2")
driver = webdriver.Chrome(options=ch_options)
driver.get("https://gmail.com")
希望这对你有所帮助,最佳