ChromeDriver不工作。
终端输出
Traceback (most recent call last):
File "crawler.py", line 7, in <module>
driver = webdriver.Chrome()
File "/usr/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/lib/python2.7/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.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/lib/chromium/chromium is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Python代码
from selenium import webdriver
import pandas as pd
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.youtube.com/user/NoCopyrightSounds/videos")
user_data = driver.find_elements_by_xpath('//*[@id="video-title"]')
links = []
for i in user_data:
links.append(i.get_attribute('href'))
print((links))
Chrome版本:80|ChromeDriver版本:80|Python版本:3.5.6|
怎么了?
我想您缺少了一些功能,请在打开chrome驱动程序之前尝试使用以下代码
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("--disable-gpu")
capabilities = options.to_capabilities()
driver = webdriver.Remote(command_executor='http://127.0.0.1:40000/wd/hub', desired_capabilities=capabilities)
driver.get('https:...')
您需要放置驱动程序,然后传递URI:
browser = webdriver.Chrome("/usr/bin/chromedriver", chrome_options=chrome_options)