显示等待
※要导入的包:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import
WebDriverWait
from selenium.webdriver.support import expected_conditions
as EC
※组成部分
try:
wait=WebDriverWait(self.driver,10,0.2)
#等待要操作的页面元素
wait.until(lambda
x:x.find_element_by_xpath(“xxxxx”))
#等待网络标题
wait.until(EC.title_is(“xxx”))
#等待alter弹框出现
wait.until(EC.alter_is_present())
#等待元素的选中状态
A=driver.find_element_by_xpath(“xxxx”)
wait.until(EC.element_to_be_selected(A))
#等待复选框的选中状态
wait.until(EC.element_to_be_clicked(By.ID,xx”))
except,TimeoutExeption,e:
print traceback.print_exc()
except,NoSuchElementException,e:
print traceBack.print_exc()
except,Exception,e:
print tranceBack.print_exc()
※代码示例如下
#encoding="utf-8"
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import
WebDriverWait
from selenium.webdriver.support import expected_conditions
as EC
from selenium.common.exceptions import TimeoutException,
NoSuchElementException
from selenium import webdriver
import time
import unittest
import traceback
class wait_(unittest.TestCase):
def
setUp(self):
self.driver = webdriver.Ie(executable_path
="c:\\Python27\\IEDriverServer")
def
test_explicitWait(self):
url="http://127.0.0.1/test_explicity_wait.html"
self.driver.get(url)
time.sleep(1)
try:
wait = WebDriverWait(self.driver,10,0.2)
#等待页面标题出现,看网页打开没
wait.until(EC.title_is(u"你喜欢的水果"))
print self.driver.title
#等待页面元素是否被勾选上
drop=self.driver.find_element_by_xpath("//select[@name='fruit']")
watermelon=drop.find_element_by_xpath(".//*[@id='watermelon']")
watermelon.click()
wait.until(EC.element_to_be_selected(watermelon))
#等待页面要操作的某个元素是否出现
wait.until(lambda
x:x.find_element_by_xpath("//input[@value='Display alert
box']")).click()
#等待页面弹框出现
wait.until(EC.alert_is_present()).text.decode("utf-8").encode("gbk","ignore")
#等待页面复选框元素是否被勾选上
clickBox=self.driver.find_element_by_id("check")
clickBox.click()
wait.until(EC.element_to_be_clickable((By.ID,'check')))
except TimeoutException,e:
print traceback.print_exc()
except NoSuchElementException,e:
print traceback.print_exc()
except Exception,e:
print traceback.print_exc()
time.sleep(2)
if
__name__=="__main__":
unittest.main()