python 显示等待_Python学习之---自动化测试框架之-----显示等待

本文详细介绍了如何利用Selenium的WebDriverWait进行网页元素的等待操作,包括等待页面加载、元素出现、选择状态等。通过示例代码展示了等待标题、元素选中、弹框及复选框状态的实现,帮助读者深入理解Selenium的隐式等待和显式等待功能。
摘要由CSDN通过智能技术生成

显示等待

※要导入的包:

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()

print

#等待页面弹框出现

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值