selenium模块,web自动化,创建浏览器
1. 创建打开浏览器
from selenium import webdriver # 用于操作浏览器 from selenium.webdriver.chrome.options import Options # 用于设置谷歌浏览器 # 创建设置浏览器对象 chrome_set = Options() # 禁用沙河模式(提高兼容性) chrome_set.add_argument("--no-sandbox") # 保持浏览器打开状态(默认代码执行完毕后自动关闭) chrome_set.add_experimental_option(name="detach", value=True) # 创建并启动浏览器 chrome = webdriver.Chrome(executable_path="chromedriver.exe", options=chrome_set)
2.打开、关闭网页
from selenium import webdriver # 用于操作浏览器 from selenium.webdriver.chrome.options import Options # 用于设置谷歌浏览器 import time def chrom(): # 创建设置浏览器对象 chrome_set = Options() # 禁用沙河模式(提高兼容性) chrome_set.add_argument("--no-sandbox") # 保持浏览器打开状态(默认代码执行完毕后自动关闭) chrome_set.add_experimental_option(name="detach", value=True) # 创建并启动浏览器 chrome = webdriver.Chrome(executable_path="chromedriver.exe", options=chrome_set) return chrome test = chrom() # 打开指定网站 test.get("https://www.baidu.com") # 关闭当前标签页 time.sleep(3) test.close() # 退出浏览器(关闭所有标签页) test.quit()
3. 浏览器最大化,最小化
# 最大化 test.maximize_window() # 最小化 test.minimize_window()
4.浏览器打开位置,打开尺寸
# 打开位置(左上角位置对应xy坐标) test.set_window_position(x=200, y=200) # 打开尺寸 test.set_window_size(width=600, height=600)
5.浏览器截图、网页刷新
# 截图 test.get_screenshot_as_file("1.png") # 刷新 test.refresh()

浙公网安备 33010602011771号