slenium 中单选框选择怎么选择

【slenium专题】Webdriver同步设置
时间: 13:04:16
&&&& 阅读:368
&&&& 评论:
&&&& 收藏:0
标签:Webdriver同步设置常用等待类主要如下图所示
注:support.ui包内类主要实现显性等待功能,timeouts()内方法主要实现隐性等待功能
一.线程休眠&
Thread.sleep(long millis)
二.隐形等待
&隐性等待:设置一次,driver整个生命周期中都在使用,不需要针对元素明确设置
driver.manage().timeouts().implicitlyWait(long outTime, TimeUnit unit);
全局设置,设置driver执行所有定位元素操作的超时时间
Parameters:outTime& 超时等待时间;unit& 时间单位.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);//超时等待时间为10S
三.显性等待
&&&&显性等待:需要等待的元素定位时需要逐一明确调用该等待方法
Figure 1 Support.ui包常用类UML
[常用等待模板]
等待元素加载
    1)WebDriverWait
new WebDriverWait(WebDriver driver, long timeOutInSeconds).until(ExpectedConditions.presenceOfElementLocated(By by))
Parameters:driver- timeOutInSeconds-超时等待时间(s); by-元素locator
timeOutInSeconds范围内,等待满足until()方法中的条件,即刻跳出等待。
超出timeOutInSeconds,抛出异常.
&    2)FluentWait&
Wait&WebDriver& wait = new FluentWait&WebDriver&(driver)
.withTimeout(long timeOut, TimeUnit unit)
.pollingEvery(long duration, TimeUnit unit)
.ignoring(exceptionType);
wait.until(ExpectedConditions.presenceOfElementLocated(By.by));
Parameters:driver-timeOut -超时等待时间(s);unit-时间单位; by-元素
duration-查找元素时间间隔 ;exceptionType-忽略异常类型,例如 默认NoSuchElementException.class
timeOut范围内,driver每隔dration定位一次元素,若遇到exceptionType则继续等待,直到满足until()方法中的条件,即刻跳出等待。
超出timeOutInSeconds仍未满足until条件,抛出异常.
&  2.等待并获取元素&仅需要修改until方法体
WebElement element = wait.until(
  new ExpectedCondition&WebElement&(){
    @Override
    public WebElement apply( WebDriver driver) {
        return driver.findElement( By by);
Parameters:driver-webdriver,从WebDriverWait|FluentWait实例化方法中获取; by-元素locator
Return:若定位到元素,返回webelement;否则报异常
等待页面元素加载
WebDriverWait
Wait&WebDriver& wait = new WebDriverWait(driver,10);
wait.until(
ExpectedConditions. presenceOfElementLocated(By.id("myDynamicElement"))
方法功能:定位id=‘ myDynamicElement‘的元素,超时等待时间为10S
FluentWait
Wait&WebDriver& wait = new FluentWait&WebDriver&(driver)
.withTimeout(60, TimeUnit.SECONDS)
.pollingEvery(10, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
方法功能:定位id=‘ myDynamicElement‘的元素,超时等待时间为60S,每隔10S定位一次,遇到NoSuchElementException报错忽略,继续等待直到超过60s。
等待并获取页面元素
WebDriverWait
Wait&WebDriver& wait = new WebDriverWait(driver, 10);
WebElement e = wait.until(
new ExpectedCondition& WebElement&(){
public WebElement apply( WebDriver d) {
return d.findElement( By.id( " myDynamicElement " ));
方法功能:定位id=‘ myDynamicElement‘的元素,超时等待时间为10S,若定位到元素,直接返回元素
&FluentWait
Wait&WebDriver& wait = new FluentWait&WebDriver&(driver)
.withTimeout(60, TimeUnit.SECONDS)
.pollingEvery(10, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
WebElement element= wait.until(
new ExpectedCondition&WebElement&() {
public WebElement apply( WebDriver d) {
return d.findElement( By.id( " myDynamicElement " ));
方法功能:定位id=‘ myDynamicElement‘的元素,超时等待时间为60S,每隔10S定位一次,60s内遇到NoSuchElementException报错忽略,继续等待;若定位到元素,直接返回该页面元素。
如果只是仅仅想判断页面是不是加载到某个地方了,就可以用第一种方法; 但如果需要得到某个WebElement,两种方式都可以,只是第一种方式还需要再多一步获取的操作.
FluentWait类是Wait接口的实现,直接使用wait接口可以更灵活的完成您需要的等待功能,例如
Wait w = new Wait(){
public boolean until() {
return webElement.isDisplayed();
这种等待的方式,据说在加载js代码的时候做判断会比较方便,目前没有调试成功。
1.等待页面加载
driver.manage().timeouts().pageLoadTimeout(100, SECONDS);
pageloadTimeout方法只适用于firefox浏览器,Chrome等其他浏览器并不适用,但我们可以自己实现一个定时器
该方法设置页面加载超时时间为100s
2.等待异步脚本加载
driver.manage().timeouts().setScriptTimeout(100,SECONDS);
尚未调试通过标签:
&&国之画&&&& &&&&chrome插件&&
版权所有 京ICP备号-2
迷上了代码!特殊功能实现-手动编辑脚本加入:
1.截图:driver.get_screenshot_as_file(&E:\Androiddriver\FirIMG\Login_1 User_Empty_W.png&)
2.窗口最大化:driver.maximize_window()
3.智能等待:self.driver.implicitly_wait(30)
4. 输出信息print:
def tearDown(self):
&&&&&& self.driver.quit()
&&&&&&&self.assertEqual([], self.verificationErrors)&&
&&&&&&&print &执行用例:Login_1 User_Empty 结果如下:& #打印出的内容可以自定义#
5.滚动条拖动:
& 1). &js=&var q=document.documentElement.scrollTop=10000& #拖动滚动条到屏幕底端
&&&&&&& driver.execute_script(js)
&2).& js=&var q=document.documentElement.scrollTop=10000& #拖动滚动条到顶端&&&&&&&
&&&&&& &driver.execute_script(js)
try: self.assertEqual(u&用户名不能为空&,driver.find_element_by_css_selector(&div.tips&).text)
except AssertionError as e:self.verificationErrors.append(str(e))#在页面上寻找内容为“用户名不能为空”的元素,如果通过则跳过except语句执行下一脚本,如果没有就执行except语句,抛出Errors
7. 处理弹出窗口:
&&&& self.assertEqual(u&登录失败&,self.close.alert_and_get_its_text())#输入错误登录信息,弹出登录失败窗口,验证提示内容及关闭该窗口
一次性执行多个用例
#-*-coding=utf-8 -*-
caselist=os.listdir('E:/F')#要执行的用例集必须在磁盘路径下、该文件以单个字母命名、各个用例依次以数字命名
for a in caselist:
&&& s=a.split('.')[1:][0]
&&& if s=='py':
&&&&&&& os.system('E:/F/%s 1&&log.txt 2&&1'%a)#表示取的是E:/F路径下所以.py文件的执行log
1.实现执行用例集的用例(如我的命名为Runall.py)放在D:\下(放在根目录下)
2. 生成的log.txt 与Runall.py同路径。
1.采用命令安装,安装完毕就可以执行脚本,selenium.py不必拷贝
2.python脚本,顶端,记得加 # -*- coding: utf-8 -*-
3.Webdriver firefox回放脚本时,退出杀毒软件;IE可以不退出
4. IE浏览器大小需设置成100% 不然会报错。
本文已收录于以下专栏:
相关文章推荐
接触过linux的人,或多或少都会了解一点make 2&&1 | tee log.txt这个命令。
  
1. make是什么?
make是linux下一个非常强大的命令,简单...
基于Selenium2与Python自动化测试环境搭建
Python版本: 选择2.7.2, Why? 目前大部分第三方库和工具对2.7都有简单的安装包,不需要自己做太多处理,比2.6内...
原文出处: http://easonhan007.github.io/python//active-python-install-selenium/
很多同学在windows搞...
1.下载并安装python,去这个地址下载最好的就好http://www.python.org/getit/,如图所示:(selenium暂时不支持python3)
2.下载安装setuptoo...
原文学习网址
我的Mac调试环境:Python2.7.12且已安装selenuim库脚本:# coding = utf-8from selenium import webdriver
import ...
WebDriver简介selenium从2.0开始集成了webdriver的API,提供了更简单,更简洁的编程接口。selenium webdriver的目标是提供一个设计良好的面向对象的API,提供...
1.webdriver对浏览器的支持
     1.1HtmlUnitDriver
          优点:打开和运行速度都很快,而且不会实际打开浏览器。
        &#1...
selenium webdriver python的路径报错的解决方法
他的最新文章
讲师:姜飞俊
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)可能大家会遇到同样的问题,用Selenium IDE录制单选框或多选框后,在脚本中,如果想根据不同的用例,选择不同的单选框或多选框的业务流程时,在WebDriver代码中如何维护呢?这里有简单处理的两种方法:
WebElement select = dirver.findElement(By.xpath(“//selenium”));
List&WebElement& allOptions = select.findElements(By.tagName(“option”));
For(WebElement option : allOptions){
System.out.println(String.format(“Value is : %s”,option.getValue()));
If(option.getValue()==”广州”){
Option.setSelected();
Select select = new Select(driver.findElement(By.xpath(“//select”)));
Select.deselectAll();
Select.selectByVisibleText(“广州”);
代码中,会从页面的第一个元素开始,对所有单选框取消选中,接着选中显示文本为“广州”的单选框。
很明显,第二种方法运行的效率比第一种高,因为第一种方法还要遍历所有的单选元素。
以为只是以单选框为例,也可以作为这种的方法去解决多选框的问题。
本文已收录于以下专栏:
相关文章推荐
#coding: utf-8
#以下代码用来遍历所有复选框
from selenium import webdriver
import time
driver = webdrive...
使用selenium定位需要注意的问题
选择框选择的方式:
(1)根椐看到的文字来选择:
Select(driver.find_element_by_name(&wl0_net_m...
代码如下:
WebElement selectElement = driver.findElement(By.name(&selectedList&));
Select select...
网页上有时候遇到checkbox和radio,一般情况下这两种都是input标签,我们可以通过点击或者发送空格的方式进行选中
试验网页代码checkandradio.html:
Checkbo...
楼主原创,分享不易,转载请注明,谢谢。
在使用selenium webdriver进行元素定位时,通常使用findElement或findElements方法结合By类返回的元素句柄来定位元素。...
转自:http://www./lib/view/open7.html
选择合适的WebDrvier
WebDriver是一个接口,它有几种实现...
转自:/qingchunjun/p/4208159.html
By.xpath()
这个方法是非常强大的元素查找方式,使用这种方法几乎可以定...
本篇介绍webdriver处理前端单选按钮的操作。单选按钮一般叫raido button,就像我们在电子版的单选答题过程一样,单选只能点击一次,如果点击其他的单选,之前单选被选中状态就会变成未选中。单...
本文介绍如何利用selenium中的方法去操作单选按钮(Radio Button)。
利用百度新闻页面两个单选按钮举例
默认是选择新闻全文,我试试在两者之前来回点击。
实际上,勾选一个单选按钮,也就...
Android RadioGroup单选框变成多选问题
他的最新文章
讲师:姜飞俊
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)selenium学习问题七:firefox下slenium&webdriver上传文件会报badStatuline的错
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

我要回帖

更多关于 单选框选择 的文章

 

随机推荐