1.安装Chrome
rpm -ivh google-chrome-stable-96.0.4664.45-1.x86_64.rpm
2.配置chromedriver
unzip chromedriver_linux64.zip mv chromedriver /usr/local/bin/
3.验证
[root@jenkins ~]
Google Chrome 96.0.4664.45
[root@jenkins ~]
[root@jenkins ~]
ChromeDriver 96.0.4664.45 (76e4c1bb2ab4671b8beba3444e61c0f17584b2fc-refs/branch-heads/4664@{
4.运行程序
import time
import platform
import logging
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
logging.basicConfig(level=logging.DEBUG)
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--ignore-certificate-errors")
chrome_options.add_experimental_option("excludeSwitches", ['enable-logging'])
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
print("启动浏览器")
driver = None
if platform.system() == "Linux":
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--window-size=1960,1080')
ser = Service(executable_path="/usr/local/bin/chromedriver")
driver = webdriver.Chrome(service=ser, options=chrome_options)
elif platform.system() == "Windows":
driver = webdriver.Chrome(options=chrome_options)
first_url = "https://www.baidu.com"
print("访问第一个页面:{url}".format(url=first_url))
driver.get(first_url)
time.sleep(2)
second_url = "https://xueshu.baidu.com"
print("访问第二个页面:{url}".format(url=second_url))
driver.get(second_url)
time.sleep(2)
print("浏览器后退:{url}".format(url=first_url))
driver.back()
time.sleep(2)
driver.get_screenshot_as_file("./100.png")
time.sleep(2)
print("浏览器前进:{url}".format(url=second_url))
driver.forward()
time.sleep(2)
driver.get_screenshot_as_file("./101.png")
time.sleep(2)
print("刷新浏览器")
driver.refresh()
time.sleep(2)
print("退出浏览器")
driver.quit()
|