简体   繁体   English

Python Selenium Chrome Webdriver 未安装

[英]Python Selenium Chrome Webdriver not installing

I am trying to automatically install the latest version of Chrome Driver and then use that for my script, but am running into errors.我正在尝试自动安装最新版本的 Chrome 驱动程序,然后将其用于我的脚本,但遇到了错误。 Any thoughts as to what's wrong here?关于这里有什么问题的任何想法? Something with my cache?我的缓存有什么东西吗?

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)

Error:错误:

[WDM] - Looking for [chromedriver 89.0.4389.23 win32] driver in cache 
[WDM] - File found in cache by path [C:\Users\xxx\.wdm\drivers\chromedriver\89.0.4389.23\win32\chromedriver.exe]
Traceback (most recent call last):
  File "C:\Users\xxx\Python\Price Tracking\Real Estate\RealEstate-Scraping.py", line 60, in <module>
    driver = webdriver.Chrome(driver2, options=options)
  File "C:\Python38\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
    self.service.start()
  File "C:\Python38\lib\site-packages\selenium\webdriver\common\service.py", line 72, in start
    self.process = subprocess.Popen(cmd, env=self.env,
  File "C:\Python38\lib\subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Python38\lib\subprocess.py", line 1247, in _execute_child
    args = list2cmdline(args)
  File "C:\Python38\lib\subprocess.py", line 549, in list2cmdline
    for arg in map(os.fsdecode, seq):
  File "C:\Python38\lib\os.py", line 818, in fsdecode
    filename = fspath(filename)  # Does type-checking of `filename`.
TypeError: expected str, bytes or os.PathLike object, not WebDriver

You need to tell the path of the webdriver:你需要告诉webdriver的路径:

webdriver.chrome(executable_path=*path*,options=options)

but driver2 = webdriver.Chrome(ChromeDriverManager().install()) creates a new instance of selenium.但是driver2 = webdriver.Chrome(ChromeDriverManager().install())创建了 selenium 的新实例。

driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)

should work with your use case - the 1st line of your code is not necessary .应该适用于您的用例 - 您的代码的第一行不是必需的。

Please note that the 'headless' needs '--' in front of it, too.请注意,“无头”也需要在它前面加上“--”。

Full code:完整代码:

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)

You're sending a WebDriver object as a positional argument.您正在发送 WebDriver object 作为位置参数。

https://www.selenium.dev/selenium/docs/api/py/webdriver_chrome/selenium.webdriver.chrome.webdriver.html#module-selenium.webdriver.chrome.webdriver https://www.selenium.dev/selenium/docs/api/py/webdriver_chrome/selenium.webdriver.chrome.webdriver.html#module-selenium.webdriver.chrome.webdriver

The first argument to WebDriver is the executable path. WebDriver 的第一个参数是可执行路径。

try that out:试试看:

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()

when you're passing argument options to webdriver, set it above initializing and put it in chrome() like that:当您将参数选项传递给 webdriver 时,将其设置在初始化之上并将其放入 chrome() 中,如下所示:

options = Options()
options.add_argument('--headless')
options.add_argument('--window-size=1920,1080')
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)

and initialize driver finally最后初始化驱动程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM