Chrome和ChromeDriver版本对应
查看自己Chrome浏览器版本:chrome://version/
去ChromeDriver网站下载对应版本的驱动
- https://sites.google.com/chromium.org/driver/downloads (官网)
- https://registry.npmmirror.com/binary.html?path=chromedriver/ (淘宝镜像)
初始化
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chromedriver = './chromedriver.exe'
options = webdriver.ChromeOptions()
# chrome_options.add_argument('--disable-gpu')
# chrome_options.add_argument('--headless')
# chrome_options.add_argument('--no-sandbox')
# chrome_options.add_argument('--disable-dev-shm-usage')
# chrome_options.add_argument('window-size=1920x1480')
options.add_argument("start-maximized")
service = webdriver.chrome.service.Service(chromedriver)
browser = webdriver.Chrome(service=service, options=options)
元素选择
find_element_by_*
已废弃,用**find_element(by, value)**
替代
- by:
By.ID
,By.CSS_SELECTOR
,By.XPATH
,By.CLASS_NAME
browser.find_element_by_css_selector('input[type="text"]')
browser.find_element(By.CSS_SELECTOR, 'input[type="text"]')
browser.find_element('css selector', 'input[type="text"]')
事件触发
# 输入框填写
browser.find_element(By.CSS_SELECTOR, 'input[type="text"]').send_keys('username')
# 清除
browser.find_element(By.CSS_SELECTOR, 'input[type="text"]').clear()
# 点击事件
browser.find_element(By.CSS_SELECTOR, 'input[type="submit"]').click()
# 截屏
browser.save_screenshot('out.png')
# 刷新页面
browser.refresh()
# 关闭当前窗口
browser.close()
# 关闭浏览器和驱动程序
browser.quit()
# 当前网页源码
browser.page_source
# soup = bs4.BeautifulSoup(browser.page_source, 'html.parser')
headless模式
chrome_options.add_argument('--headless')
cookies读写
# dump
cookies = browser.get_cookies()
# json.dump to a file
# load
# json.load cookies from a file
for cookie in cookies:
browser.add_cookie(cookie)
等待
隐式模式
browser.implicitly_wait(30)
显示模式
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(browser, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input:nth-child(1)'))).click()
wait.until(EC.element_to_be_selected((By.CSS_SELECTOR, 'ul li:nth-child(3)'))).click()