[python,chrome驱动程序错误

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

我有这个文件test.py:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://www.google.com")
print(driver.page_source)
driver.quit()

我想使用此python代码从页面获取页面源。我正在使用chrome驱动程序selenium和python。但是,当我启动此脚本时,出现此错误:

 File "test2.py", line 8, in <module>
    driver = webdriver.Chrome(chrome_options=options)
  File "/usr/local/python3/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/usr/local/python3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/python3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/python3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/python3/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: cannot create temp dir for user data dir

我几乎尝试了Internet上的所有解决方案,但是每次遇到其他错误时,有人知道我在这种情况下应该采取的措施,以及有关如何解决此问题的建议吗?

python selenium google-chrome
2个回答
0
投票

请确保您已安装正确的Chrome浏览器和正确的chromedriver支持版本这是一些代码编辑建议

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.headless = True
options.add_argument('--disable-gpu')

driver = webdriver.Chrome(options=options)
driver.get("https://www.google.com")
print(driver.page_source)
driver.quit()

0
投票

尝试设置路径驱动程序,例如波纹管...。使用以下命令定位:哪里是chromedriver

from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('/usr/bin/chromedriver',chrome_options=chrome_options)
热门问题
推荐问题
最新问题