python爬虫学习之selenium_chrome handless的使用

目录

一、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 Options

chrome_options = Options()
chrome_options. add_argument('--headless')

#path是自己电脑的Chrome浏览器文件地址
path = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
chrome_options.binary_location = path

browser = webdriver.Chrome(options=chrome_options)

 四、Chrome handless 的应用

e.g.访问百度网站

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options. add_argument('--headless')

#path是自己电脑的Chrome浏览器文件地址
path = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
chrome_options.binary_location = path

browser = 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 Options

def 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 = path

    browser = webdriver.Chrome(options=chrome_options)
    return browser

注:使用时直接调用函数

browser = common_browser()

url = 'https://www.baidu.com'

browser.get(url)

猜你喜欢

转载自blog.csdn.net/weixin_73002278/article/details/135906769