我试图自动安装最新版本的Chrome驱动程序,然后将其用于我的脚本,但遇到了错误。有什么想法吗?我的缓存有什么?
driver2 = webdriver.Chrome(ChromeDriverManager().install())
options = selenium.webdriver.ChromeOptions()
#options.add_argument('headless')
options.add_argument('window-size=1920x1080')
driver = webdriver.Chrome(driver2, options=options)
错误:
[WDM] - Looking for [chromedriver 89.0.4389.23 win32] driver in cache
[WDM] - File found in cache by path [C:Usersxxx.wdmdriverschromedriver89.0.4389.23win32chromedriver.exe]
Traceback (most recent call last):
File "C:UsersxxxPythonPrice TrackingReal EstateRealEstate-Scraping.py", line 60, in <module>
driver = webdriver.Chrome(driver2, options=options)
File "C:Python38libsite-packagesseleniumwebdriverchromewebdriver.py", line 73, in __init__
self.service.start()
File "C:Python38libsite-packagesseleniumwebdrivercommonservice.py", line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "C:Python38libsubprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:Python38libsubprocess.py", line 1247, in _execute_child
args = list2cmdline(args)
File "C:Python38libsubprocess.py", line 549, in list2cmdline
for arg in map(os.fsdecode, seq):
File "C:Python38libos.py", line 818, in fsdecode
filename = fspath(filename) # Does type-checking of `filename`.
TypeError: expected str, bytes or os.PathLike object, not WebDriver
您需要告诉webdriver的路径:
webdriver.chrome(executable_path=*path*,options=options)
但是driver2 = webdriver.Chrome(ChromeDriverManager().install())
创建硒的新实例
driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)
应该与您的用例一起使用-代码的第一行是,而不是必需的。
请注意,"无头"也需要前面的"--"。
完整代码:
options = selenium.webdriver.ChromeOptions()
#options.add_argument('--headless')
#could also do options.headless = True
options.add_argument('--window-size=1920x1080')
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
driver.get('enterwebsite.ext')
#do other stuff
driver = webdriver.Chrome(driver2, options=options)
您正在发送一个WebDriver对象作为位置参数。
https://www.selenium.dev/selenium/docs/api/py/webdriver_chrome/selenium.webdriver.chrome.webdriver.html#module-硒.webdriver.chrome.webdriver
WebDriver的第一个参数是可执行路径。
尝试一下:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
options = Options()
options.add_argument('--headless')
options.add_argument('--window-size=1920,1080')
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
driver.get('https://google.com')
driver.quit()
当您将参数选项传递给webdriver时,将其设置在初始化之上,并将其放入chrome((中,如下所示:
options = Options()
options.add_argument('--headless')
options.add_argument('--window-size=1920,1080')
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
并最终初始化驱动程序