python selenium的一些笔记

很多固定的参数有时候忘了,用的时候找不到代码在哪里,现在就记一下。

很有可能版本的不同,导致参数的失效。

以这个测试网站为例子: https://bot.sannysoft.com/
查看参数是否设置成功。

"""
https://bot.sannysoft.com/

# 下载对应的驱动
https://chromedriver.storage.googleapis.com/index.html?path=88.0.4324.96/

版本
selenium         3.141.0
chromedriver 88.0.4324.96
"""


import time

from selenium import webdriver
chrome_options = webdriver.ChromeOptions()

# chrome_options.add_argument("--headless")  # 无头模式浏览器
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])  # 隐藏受控制提示
chrome_options.add_experimental_option('useAutomationExtension', False)
chrome_options.add_argument("--disable-blink-features")  # 隐藏webdriver
chrome_options.add_argument("--disable-blink-features=AutomationControlled")

# 设置代理
# chrome_options.add_argument("--proxy-server=http://202.20.16.82:10152")

# 这里 chromedriver 需要在上面的地址下载不同电脑的驱动
driver = webdriver.Chrome(executable_path="./chromedriver", chrome_options=chrome_options)

driver.set_window_size(1480, 1300)  # 分辨率 1280*800
# driver.maximize_window()  # 全屏

driver.get("https://bot.sannysoft.com/")

time.sleep(5)
driver.implicitly_wait(10)  # seconds


# 切换新打开的tab页面
# driver.switch_to.window(driver.window_handles[1])

print(driver.title)

driver.close()
原文地址:https://www.cnblogs.com/CharmCode/p/14343361.html