如何调整以下代码以在无头模式下运行?



我正在使用以下代码:

import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
PATH= r"C:UsersHamidDesktopSeleniumchromedriver.exe"
driver=webdriver.Chrome(PATH)
driver.get("https://www.google.com/")
click_button=driver.find_element_by_xpath('//*[@id="L2AGLb"]/div').click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q"))).send_keys("ONS data")
search=driver.find_element_by_xpath('/html/body/div[1]/div[3]/form/div[1]/div[1]/div[3]/center/input[1]').click()

我怎样才能使它在无头模式下运行?

似乎您正在使用Selenium v3.x。所以要使用无头模式你只需要将--headless属性设置为true通过Options()类实例,如下所示:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
PATH= r"C:UsersHamidDesktopSeleniumchromedriver.exe"
driver=webdriver.Chrome(executable_path=PATH, options=options)
driver.get("https://www.google.com/")
click_button=driver.find_element_by_xpath('//*[@id="L2AGLb"]/div').click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q"))).send_keys("ONS data")

相关内容

  • 没有找到相关文章

最新更新