如何隱藏火狐視窗(Selenium WebDriver)?
我們可以在 Selenium webdriver 中隱藏火狐視窗。可以透過讓瀏覽器無頭來完成此操作。我們將使用FirefoxOptions 類來實現此操作。然後我們將針對該類建立一個物件option。
我們必須將瀏覽器設定options.headless設為True 值。然後,此驅動程式物件將接收此資訊。我們需要具有以下匯入語句:import Options as FirefoxOptions from selenium.webdriver.firefox.options,以新增 FirefoxOptions 類。
語法
options = webdriver.FirefoxOptions() options.headless = True
舉例
程式碼實現。
from selenium import webdriver from selenium.webdriver.firefox.options import Options as FirefoxOptions #object of FirefoxOptions options = webdriver.FirefoxOptions() #setting headless parameter options.headless = True driver = webdriver.Firefox(executable_path="C:\geckodriver.exe", options=options) driver.implicitly_wait(0.8) driver.get("https://tutorialspoint.tw/tutorialslibrary.htm") #identify element n = driver.find_element_by_xpath("//*[text()='Library']") #perform click n.click(); print("Page title after click: " + driver.title) driver.quit()
輸出
廣告