import time
from selenium import webdriver
driver = webdriver.Chrome('C:\Program Files\Google\Chrome\Application\chrome.exe')
driver.get('https://www.facebook.com')
time.sleep(5)
driver.quit()
錯誤代碼:Executable_path 已被棄用,請傳入一個服務物件。
上面的代碼開始打開一個谷歌瀏覽器標簽,但沒有選擇用戶,它會在谷歌瀏覽器顯示所有用戶的地方停止。我嘗試使用特定的組態檔路徑,但出現了各種錯誤。如果有人能夠解決此問題,我將不勝感激,我想以訪客身份打開 Chrome 標簽。
uj5u.com熱心網友回復:
看起來你的問題有兩個部分。您正在嘗試找出 webdriver 和用戶組態檔路徑。請允許我為您回答這兩個問題。
在最新版本的 Selenium 中,該executable_path引數已被棄用。現在需要包含可執行路徑的服務物件。有兩種選擇。
服務物件
選項 #1:使用您的可執行路徑
將此匯入附加到您的代碼中:
from selenium.webdriver.chrome.service import Service
然后,像這樣包含服務物件:
driver = webdriver.Chrome(service=Service("C:\Program Files\Google\Chrome\Application\chrome.exe"))
選項 #2:讓 Web 驅動程式管理器處理它
當驅動程式過時時,這非常有用。無需重新下載驅動程式。
首先,轉到終端中的專案目錄。如果您使用的是 PyCharm,則無需遍歷目錄,因為您已經在專案目錄中。
使用 pip 安裝 web 驅動管理器:
pip install webdriver_manager
現在,無需輸入可執行路徑:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.facebook.com")
選擇用戶組態檔
這相當簡單。首先,進入chrome并進入chrome://version/URL地址欄。您將看到組態檔路徑。它看起來像這樣C:\Users\yourprofile\AppData\Local\Google\Chrome\User Data\Default。
然后,包括以下 chrome 選項:
options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\Users\yourprofile\AppData\Local\Google\Chrome\User Data")
options.add_argument(r"--profile-directory=Default")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
uj5u.com熱心網友回復:
這對我有用。該服務是您可以下載的 chrome 驅動程式的路徑。chrome驅動可以在這里下載:https ://chromedriver.chromium.org/downloads
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
s = Service('/Users/macbook/PycharmProjects/chromedriver')
browser = webdriver.Chrome(service=s)
browser.get('https://www.facebook.com')
time.sleep(5)
browser.quit()
[1]: https://chromedriver.chromium.org/downloads
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/483359.html
