Selenium基本用法汇总

用法声明

# coding=utf-8
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
import time

url = 'http://www.baidu.com'
## firefox & chrome webdriver
## chrome webdriver: https://chromedriver.chromium.org/downloads 注意对应chrome版本
driver = webdriver.Firefox()
driver = webdriver.Chrome(driver_path, chrome_options = webdriver.ChromeOptions().add_argument("--incognito")) # 无痕窗口
# driver.minimize_window()
driver.get(url)

三种等待方式

## https://huilansame.github.io/huilansame.github.io/archivers/sleep-implicitlywait-wait
time.sleep(5)   # 强制等待:5s
driver.implicitly_wait(5)   $ 隐式等待:5s内页面加载完成后执行后续动作
WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, 'login-name')))  # 显示等待:5s内直至元素符合条件后执行定位

几种元素定位方式

ele = driver.find_element_by_xpath('//*[@id="username"]').send_keys(username) # 右键检查/F12 -> 定位标签 -> 右键copy -> copy xpath
ele = driver.find_element_by_id('element_id')   # 注意AJAX下id可能会变动, 相应的xpath也可能变动
ele = driver.find_element_by_css_selector('.class1.class2.class3')  # 灵活
ele = driver.find_element_by_class_name('classname')    # 方便,但可能定位不唯一

ele = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, 'login-name')))
ele = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.CLASS_NAME, 'classname')))
ele = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="login_form"]/div/div[3]/button')))
ele = WebDriverWait(driver, 5).until_not(EC.presence_of_element_located((By.CSS_SELECTOR, '.class.hide')))
## AJAX下元素定位可能一直为真,但也许是语义错误,此时需要time.sleep()

元素操作

ele.click() # 按钮点击
ele.sendkey('username') # 输入框传值
ele.get_attribute('para_name') # 'para_name' in ('class', 'innerText', 'textContent', 'innerHTML') # 获取文本

基本异常

try:
    ele = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH , xpath)))
except TimeoutException:
    print ("xpath: Loading took too much time!")
    pass # 忽略

参考

https://stackoverflow.com/questions/49775502/webdriverwait-not-working-as-expected/49775808#49775808
https://stackoverflow.com/questions/57262217/how-do-you-use-ec-presence-of-element-locatedby-id-mydynamicelement-excep

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章