python+selenium打开浏览器时报错

在Ubuntu下,用python3.5 + selenium 3.9.0 ,打开浏览器的时候想载入cookie,查了下win下
的目录,是在:C:UsersusernameAppDataLocalGoogleChromeUser Data,然后我参照这个找到我的目录是在:/home/username/.config/google-chrome下(理由是这俩目录下的文件结构几乎一样)。

但是这样运行之后就报错了。错误如下:

File "test.py", line 18, in <module>

driver=webdriver.Chrome(chrome_options=chrome_options)

File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 75, in init

desired_capabilities=desired_capabilities)

File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 154, in init

self.start_session(desired_capabilities, browser_profile)

File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session

response = self.execute(Command.NEW_SESSION, parameters)

File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute

self.error_handler.check_response(response)

File "/usr/local/lib/python3.6/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: exited abnormally

下面是我的代码:

from selenium import webdriver
import os
import time
from pyvirtualdisplay import Display

cookie_dir= '/home/username/.config/google-chrome' # 对应你的chrome的用户数据存放路径
chrome_options=webdriver.ChromeOptions()
chrome_options.add_argument('--profile-directory=Default')
chrome_options.add_argument("--user-data-dir="+os.path.abspath(cookie_dir))

driver=webdriver.Chrome(chrome_options=chrome_options)
driver.maximize_window()
driver.get("https://www.baidu.com/")

如果把 chrome_options.add_argument("--user-data-dir="+os.path.abspath(cookie_dir)) 这一行去掉,就能正常打开。
下面是代码的截图:
图片描述
请问有大神碰到这个情况么?感激不尽

以上是 python+selenium打开浏览器时报错 的全部内容, 来源链接: utcz.com/a/162597.html

回到顶部