「Python」Selenium-Chrome handless

时间:2024-02-18 20:37:42
  1. 基本代码
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')

browser = webdriver.Chrome(options=chrome_options)
  1. 封装配置
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


def share_browser():
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')

    browser = webdriver.Chrome(options=chrome_options)

    return browser
"""
Chrome handless

Author:binxin
Date:2023/11/30 11:06
"""
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


def share_browser():
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')

    browser = webdriver.Chrome(options=chrome_options)

    return browser


# 封装调用
browser = share_browser()
url = 'http://www.baidu.com/'
browser.get(url)
browser.save_screenshot('baidu.png')