[英]Issues with selenium in Python (Maybe installing chrome webdriver)
[英]Python Selenium Chrome Webdriver not installing
我正在嘗試自動安裝最新版本的 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:\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
你需要告訴webdriver的路徑:
webdriver.chrome(executable_path=*path*,options=options)
但是driver2 = webdriver.Chrome(ChromeDriverManager().install())
創建了 selenium 的新實例。
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 object 作為位置參數。
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)
最后初始化驅動程序
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.