环境
Python 3.8.6
Selenium 3.8.1
Chromedriver 103.0.5060.24
Windows 11
解释selenium操控chrome高级操作
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.chrome.options import Options
import unittest, time, re
class DownLoad(unittest.TestCase):
def setUp(self):
options = webdriver.ChromeOptions()
out_path = r""
options.add_experimental_option("prefs", {
"profile.default_content_settings.popups": 0, # 默认设置窗口
"download.default_directory": out_path, # 默认目录
"download.prompt_for_download": False, # 是打开询问保存文位置
"download.directory_upgrade": True, # 目录升级
"safebrowsing.enabled": False # 安全浏览启用
})
# options.add_argument('--user-data-dir=/Users/user_data')
options.add_argument('--user-data-dir=D:\\KDT_test\\userdata') # 保存当前状态
self.driver = webdriver.Chrome(chrome_options=options)
self.driver.implicitly_wait(30)
self.accept_next_alert = True
def test_download(self):
driver = self.driver
driver.get("https://adaad")
driver.find_element(By.ID, "UserID").click()
driver.find_element(By.ID, "UserID").send_keys("username")
driver.find_element(By.ID, "Paswd").click()
driver.find_element(By.ID, "Paswd").send_keys("passwoed")
driver.find_element(By.ID, "Paswd").send_keys(Keys.ENTER)
driver.find_element(By.LINK_TEXT, "down_load").click()
driver.switch_to.frame(1)
driver.find_element(By.LINK_TEXT, "导出").click()
time.sleep(5)
driver.quit()
ChromeOptions options = new ChromeOptions();
options.BinaryLocation = sysPath + "/Chrome/Chrome.exe"; ---------设置浏览器程序位置
options.AddArgument("url=data:,"); ----------- 设置启动浏览器空白页
options.AddArgument("--headless"); ----------- 隐藏浏览器
options.SetLoggingPreference("off", LogLevel.Off); ------------- 禁止 console.log 日志输出
IWebDriver driver = new ChromeDriver(sysPath + "/Chrome/", options); -------------- 设置chromedriver.exe 文件夹路径
chrome_options.add_argument('--no-sandbox')#解决DevToolsActivePort文件不存在的报错
chrome_options.add_argument('window-size=1920x3000') #指定浏览器分辨率
chrome_options.add_argument('--disable-gpu') #谷歌文档提到需要加上这个属性来规避bug
chrome_options.add_argument('--hide-scrollbars') #隐藏滚动条, 应对一些特殊页面
chrome_options.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 提升速度
chrome_options.add_argument('--headless') #浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败
详细参数:https://peter.sh/experiments/chromium-command-line-switches/
https://www.cnblogs.com/c-x-a/p/8572457.html
https://www.cnblogs.com/yangjintao/p/10599868.html
https://blog.csdn.net/qq_26200629/article/details/86141131