Selenium Driver设置
去掉“Chrome正受***”
python版:
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
drvier = webdriver.Chrome(options=chrome_options)
一个函数适应所有元素查找方式
python版
from selenium.webdriver.common.by import By
from selenium import webdriver
driver = webdriver.Chrome()
driver.find_element(By.ID, "id01")
一个函数让所有元素查找执行等待
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import TimeoutException
def find_element_by_locator(dri, locator):
if not isinstance(locator, tuple):
raise TypeError
try:
by, value = locator
ele = WebDriverWait(dri, 5, poll_frequency=1).until(
lambda d: d.find_element(by, value))
except TimeoutException:
raise AssertionError("元素未找到!")
else:
return ele
-------------------------------------------------------------
driver = webdriver.Chrome()
ele_locator = (By.ID, "id01")
element = find_element_by_locator(driver, ele_locator)
Selenium禁用Chrome密码保存提示框
python版
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
pref = {"credentials_enable_service": False, "profile.password_manager_enabled": False}
chrome_options.add_experimental_option("prefs", pref)
drvier = webdriver.Chrome(options=chrome_options)
自定义Chrome浏览器下载路径
from selenium import webdriver
import os
download_path = os.getcwd()
chrome_options = webdriver.ChromeOptions()
pref = {"download.default_directory": download_path}
chrome_options.add_experimental_option("prefs", pref)
drvier = webdriver.Chrome(options=chrome_options)
Chrome Option设置
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
chrome_options = Options()
#加上下面两行,解决报错
chrome_options.add_argument("--start-maximized") #默认打开窗口最大化
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
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下如果系统不支持可视化不加这条会启动失败
# chrome_options.binary_location = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" #手动指定使用的浏览器位置
driver=webdriver.Chrome(chrome_options=chrome_options)#executable_path驱动路径
driver.get('http://www.baidu.com')
print(driver.page_source)