chrome headless browser.png

安装selenium

pip install selenium

安装chromedriver

查看本地chrome浏览器版本,安装对应的chromedriver chromedriver下载地址

示例代码

# -*- coding: utf-8 -*-

import os

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

from selenium.webdriver.chrome.options import Options

import time

chrome_options = Options()

chrome_options.add_argument("--headless")

base_url ="http://www.baidu.com/"

#对应的chromedriver的放置目录

driver = webdriver.Chrome(executable_path=(r'C:\Program Files\Google\Chrome\Application\chromedriver.exe'), chrome_options=chrome_options)

driver.get(base_url +"/")

start_time=time.time()

print('this is start_time ',start_time)

driver.find_element_by_id("kw").send_keys("selenium webdriver")

driver.find_element_by_id("su").click()

driver.save_screenshot('screen.png')

driver.close()

end_time=time.time()

print('this is end_time ',end_time)