目录
一、Chrome handless简介
二、Chrome handless的系统要求
三、Chrome handless的基本配置 (直接复制放在.py文件开头)
四、Chrome handless 的应用
五、Chrome handless的封装
一、Chrome handless简介
Chrome handless
模式,Google
针对 Chrome
浏览器 59版
新增的一种模式,可以让你不打开 UI
界面的情况下使用 Chrome
浏览器,所以运行效果与 Chrome
保持完美一致。
二、Chrome handless的系统要求
1、Chrome 版本要求
Unix/Linux 系统要求 chrome >= 59
Windows 系统需要 chrome >= 60
2、Python 版本 >= 3.6
3、Selenium 版本 >= 3.4.*
4、ChromeDriver 版本 >= 2.31
三、Chrome handless的基本配置 (直接复制放在.py文件开头)
from selenium import webdriver
from selenium.webdriver.chrome.options import Optionschrome_options = Options()
chrome_options. add_argument('--headless')#path是自己电脑的Chrome浏览器文件地址
path = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
chrome_options.binary_location = pathbrowser = webdriver.Chrome(options=chrome_options)
四、Chrome handless 的应用
e.g.访问百度网站
from selenium import webdriver
from selenium.webdriver.chrome.options import Optionschrome_options = Options()
chrome_options. add_argument('--headless')#path是自己电脑的Chrome浏览器文件地址
path = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
chrome_options.binary_location = pathbrowser = webdriver.Chrome(options=chrome_options)url = 'https://www.baidu.com'browser.get(url)
注:chrome_options已经被options替代
五、Chrome handless的封装
from selenium import webdriver
from selenium.webdriver.chrome.options import Optionsdef common_browser():chrome_options = Options()chrome_options.add_argument('--headless')# path是自己电脑的Chrome浏览器文件地址path = r'C:\Program Files\Google\Chrome\Application\chrome.exe'chrome_options.binary_location = pathbrowser = webdriver.Chrome(options=chrome_options)return browser
注:使用时直接调用函数
browser = common_browser()url = 'https://www.baidu.com'browser.get(url)