簡體   English   中英

硒腳本不在python中運行

[英]selenium script doesn't run in python

嘗試執行

from selenium import webdriver

browser=webdriver.Chrome()

browser.get('http://www.google.com')

但不執行並引發錯誤

=RESTART: C:\Users\Phani\AppData\Local\Programs\Python\Python37-32\drive.py =
Traceback (most recent call last):
  File "C:\Users\Phani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
    stdin=PIPE)
  File "C:\Users\Phani\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "C:\Users\Phani\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1178, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

在處理上述異常的過程中,又出現了一個異常:

Traceback (most recent call last):
  File "C:\Users\Phani\AppData\Local\Programs\Python\Python37-32\drive.py", line 4, in <module>
    browser=webdriver.Chrome()
  File "C:\Users\Phani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
    self.service.start()
  File "C:\Users\Phani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\common\service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

使用 pip install 正確安裝 selenium

請幫忙!!

這不起作用,因為您沒有提供 chrome 驅動程序的路徑。 WebDriver不知道chromedriver路徑在哪里。

driver = webdriver.Chrome("path of the chrome driver Chromedriver.exe")

例如:

from selenium import webdriver
driver = webdriver.Chrome("d:/chromedriver/Chromedriver.exe")
driver.get("https://www.google.com")

為此,您可以嘗試使用 Firefox:

首先從這里下載 Geckodriver: https ://github.com/mozilla/geckodriver/releases/tag/v0.24.0

from selenium import webdriver
fox = webdriver.Firefox(executable_path='/path/to/downloaded/gecko/driver')
fox.get("https://www.google.com")

google chrome也可以這樣做:

您可以從這里下載 chrome 驅動程序:

https://sites.google.com/a/chromium.org/chromedriver/home

無需設置 PATH 變量。

我剛剛解決了這個問題。 您需要下載 Google Chrome 的更新版本。 例如,我的舊版本是 96,但現在是 101。就是這樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM