1. import packages
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
2. 元素操作
清空文本
elm.clear()
输入文本
elm.send_keys(‘xxxx‘)
上传文件
------type = ‘file‘-----
elm.send_keys(r‘/locate‘)
执行js
driver.execute_script()
切换浏览器
- 获取所有的浏览器窗口:wins = driver.window_handles
- driver.switch_to.wins(wins[-1])
driver.switch_to.wins(wins[0])
切换iframe
-
鼠标操作
- from selenium.webdriver.common.action_chains import ActionChains
- ActionChains(driver).move_to_element(elm).perform()
- ActionChains(driver).move_to_element(elm).click().perform()
etc
拖拽行为
- action = ActionChains(driver)
action.move_to_element(elm).click_and_hold().move_by_offset(x,y).release().perform()
隐式等待
- driver.implicitly_wait(x)
如果在x之前就加载出来,则不再等待
如果超过x未加载出来,则抛超时异常
显示等待
- from selenium.webdriver.common.by import By
from selenium.webedriver.support.wait import WebdriverWait
WebdriverWait(driver,15).until(EC.presence_of_element_located((By.Xpath,‘loc‘)))
隐式等待是针对所有元素
显示等待只针对特定元素
原文:https://www.cnblogs.com/lutong1989/p/14884293.html