selenium中查找页面的方法
1.通过id查找
driver.find_element_by_id()
如:
driver.find_element_by_id('kw')
2.通过css选择器查找
driver.find_element_by_css_selector()
id:#id值
class:.class值
标签:标签名
如:
driver.find_element_by_css_selector('#kw')
3.通过xpath查找
driver.find_element_by_xpath(xpath语法)
等待的种类
1.强制等待
time.sleep()
2.隐式等待
##隐式等待就是等到页面全部加载完成,比如js,css或者图片全请求到加载到页面,也就是我们常看到的页面不在转圈圈为止,程序才会继续运行。
driver.implicitly_wait(10)
3.显式等待
## 导包
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
## 步骤:
## 1.创建等待对象
wait = WebDriverWait(
driver,#浏览器驱动对象
10,最大等待时长
0.5,扫描间隔
)
## 2.wait.until(等待条件)--->等待条件成立程序才继续运行。
### 等待条件在selenium中有个专门的模块来设置了一些条件-------->expected_conditions as EC
### 最常用的条件有一下两个:
EC.presence_of_element_located((
查找的方法:By.ID,By.XPATH,By.CAA_SELECTOR,
查找的方法对应的查找语法
))------->只要有一个符合条件的元素加载出来就通过
EC.presence_of_all_elements_located((
查找的方法:By.ID,By.XPATH,By.CAA_SELECTOR,
查找的方法