ui自动化---select标签和浏览器等待

一、select

引入模块from selenium.webdriver.support.select import Select

Select(select).select_by_value('4')#通过select标签的value进行选择
Select(select).select_by_index(0)#通过select标签的index进行选择

#一个标签中如果还有子集,可以继续在这个标签的基础上进行find
opts=select.find_elements_by_tag_name('option')#找到select下的所有option标签
for opt in opts:
    print(opt.get_attribute('value'))

二、等待

1、通常我们用的等待就是time模块的time.sleep()

2、第二种就是隐式等待:

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

#10s内,没个0.5s扫描,元素不可见或者找不到,返回true。如果可见返回错误
WebDriverWait(driver,10,0.5).until(EC.invisibility_of_element_located((By.ID,'i1')))

# 10s内,每隔1s扫描,until是否找到元素,如果找到则返回该元素,否则报错
el=WebDriverWait(driver,10,1).until(EC.presence_of_element_located((By.ID,'i11')))
el.send_keys('xxx')
WebDriverWait(driver,10,0.5).until(EC.invisibility_of_element_located((By.ID,'i1')))举例如下



猜你喜欢

转载自www.cnblogs.com/bendouyao/p/9302486.html