我目前正在學習 Selenium 4.0 并設定了一個基本腳本,該腳本將單擊 Python 網站上的按鈕。我正在使用 Chrome 網路驅動程式。但是,每當我運行我的代碼時,都會在 Python 網站上打開一個 chrome 視窗,然后立即關閉。如何保持打開狀態?
瀏覽器版本和webdriver版本是一樣的,連Edge webdriver都試過了,重裝了Chrome。我什至嘗試將網路驅動程式下載到我的本地目錄,但這也不起作用。這是我當前的腳本:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
service = ChromeService(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
driver.get("https://www.python.org/")
print(driver.title)
submit = driver.find_element(By.ID, "submit")
submit.click()
運行后,我的終端給出以下訊息:
====== WebDriver manager ======
Current google-chrome version is 101.0.4951
Get LATEST chromedriver version for 101.0.4951 google-chrome
Driver [/Users/user1/.wdm/drivers/chromedriver/mac64_m1/101.0.4951.41/chromedriver] found in cache
Welcome to Python.org
Process finished with exit code 0
uj5u.com熱心網友回復:
好吧,這是正確的行為,因為它會執行您告訴它正確執行的所有操作。事實上,你沒有收到任何錯誤。執行代碼后,Chrome 驅動程式被殺死,因為 Python 應用程式完成執行
如果您希望驅動程式打開的瀏覽器保持打開狀態,請使用 Chrome 選項并添加分離
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/482280.html
