我如何使用无头浏览器 运行 selenium python

How I can run selenium python with headless browser

我已经使用 python selenium 做了一些测试,但我不希望它打开任何浏览器,那么我如何使用无头浏览器呢?你们能帮帮我吗:(( 这是我的代码:

from selenium import webdriver
from selenium.webdriver.support.ui import Select

driver = webdriver.Chrome('/Users/toanhac/Downloads/chromedriver')


url = 'https://mail.google.com/mail/u/0/#inbox'
driver.get(url)

driver.implicitly_wait(15) 


driver.find_element_by_id("identifierId").send_keys(mail)
driver.find_element_by_id("identifierNext").click()

driver.find_element_by_name("password").send_keys(password)
driver.find_element_by_id("passwordNext").click()

只需将 headless 设置为 True:

from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True 

完整代码:

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options

options = Options()
options.headless = True 

driver = webdriver.Chrome('/Users/toanhac/Downloads/chromedriver', options=options)


url = 'https://mail.google.com/mail/u/0/#inbox'
driver.get(url)

driver.implicitly_wait(15) 


driver.find_element_by_id("identifierId").send_keys(mail)
driver.find_element_by_id("identifierNext").click()

driver.find_element_by_name("password").send_keys(password)
driver.find_element_by_id("passwordNext").click()