1. 程式人生 > >Chrome與Firefox的headless 模式與selenium對接

Chrome與Firefox的headless 模式與selenium對接

因為PhantomJS年久失修,selenium已將其標記為不可用(雖然實際上還能執行):

有必要了解一下其他兩個瀏覽器的headless 模式。

Chrome的headless模式:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
# chrome_options.binary_location = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
browser = webdriver.Chrome(chrome_options=chrome_options)
browser.get('https://www.baidu.com')
print(browser.current_url)
print(browser.title)

Firefox的headless模式:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
firefox_options = Options()
firefox_options.add_argument('--headless')
# chrome_options.binary_location = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
browser = webdriver.Firefox(firefox_options=firefox_options)
browser.get('https://www.baidu.com')
print(browser.current_url)
print(browser.title)