1.1使用JavaScriptExecutor单击元素

  被测网页的网址:

  http://www.baidu.com

  Java语言版本的API实例代码

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod; public class TestDemo {
WebDriver driver;
String url = "http://www.baidu.com";
@Test
public void testHandleiFrame() throws Exception {
WebElement searchInputBox = driver.findElement(By.id("kw"));
WebElement searchButton = driver.findElement(By.id("su"));
searchInputBox.sendKeys("使用JavaScript语句进行页面元素单击");
JavaScriptClick(searchButton);
}
public void JavaScriptClick(WebElement element)throws Exception {
try {
//判断传入的参数是否处于页面上及是否可单击
if(element.isEnabled() && element.isDisplayed()){
System.out.println("使用JavaScript语句进行页面元素单击");
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element); }else{
System.out.println("页面上的元素无法进行单击操作");
}
} catch (StaleElementReferenceException e) {
System.out.println("页面元素没有附加在网页中"+e.getStackTrace());
}catch(NoSuchElementException e){
System.out.println("在页面中没有找到要操作的页面元素"+e.getStackTrace());
}catch(Exception e){
System.out.println("无法完成单击动作"+e.getStackTrace());
} }
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
} @AfterMethod
public void afterMethod() {
driver.quit();
} }

  代码解释:

  JavaScriptClick方法里面的代码实现的是一种封装,把常用的操作写在一个函数方法里,可以方便调用,减少冗余代码的编写,提高测试代码的编写效率

  1.2在Ajax方法产生的浮动框中,单击选择包含某个关键字的选项

  被测网页的网址:

  https://www.sogou.com/

  Java语言版本的API实例代码

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; public class Ajax {
WebDriver driver;
String url = "https://www.sogou.com/";
@Test
public void testAjaxDivOption() throws Exception {
//设置显示等待最长为10秒
WebDriverWait wait = new WebDriverWait(driver,10);
WebElement serchInputBox = driver.findElement(By.id("query"));
serchInputBox.click();
/*因为浮动框的内容总是发生改变如果想选择其中的第几项可以参考注释代码
WebElement serchInputBox1 = driver.findElement(By.xpath("//*[@id='vl']/div[1]/ul/li[1]"));
serchInputBox1.click();*/
//显示等待浮动框的第一个值 防止因页面加载未找到元素而导致失败,未等待可以会因为程序运行过快浮动框的值还未出现,后面的列表可能会取不到值
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='vl']/div[1]/ul/li[1]")));
List<WebElement>suggetionOptions = driver.findElements(By.xpath("//*[@id='vl']/div[1]/ul/li"));
//遍历浮动框的值
for(WebElement element:suggetionOptions){
//找到指定内容项
if(element.getText().contains("康宁研发柔性玻璃")){
System.out.println(element.getText());
element.click();
break;
}
} }
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
} @AfterMethod
public void afterMethod() {
driver.quit();
}
}

  代码解释:

  此代码主要是测试页面包含Ajax的局部刷新机制,产生显示多条数据的浮动框,因为浮动框的内容总是发生改变如果想选择其中的第几项可以参考注释代码通过数字进行选择

  1.3设置一个页面的属性

  被测网页的网址源码: 

  <html>
    <head>
      <title>设置文本框属性</title>
    </head>
    <body>
      <input id='text' type='text' value='今年夏天西瓜相当甜!' size=100/>文本框</input>
    </body>
  </html>

  Java语言版本的API实例代码  

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; public class dataPicker {
WebDriver driver;
String url = "file:///E:/%E6%9D%90%E6%96%99/selenium/%E5%B1%9E%E6%80%A7.html";
@Test
public void testdataPicker() {
WebElement textInputBox = driver.findElement(By.id("text"));
//调用方法改变文本框中的文字
setAttribute(driver,textInputBox,"value","文本框的文字属性和长度已被改变");
//调用方法改变文本框的长度
setAttribute(driver, textInputBox, "size", "10");
//删除文本框的size属性
removeAttribute(driver,textInputBox,"size");
} public void setAttribute(WebDriver driver, WebElement element, String attributeName, String value) {
//调用JavaScript代码修改页面元素的属性
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute(arguments[1],arguments[2])", element,attributeName,value);
}
public void removeAttribute(WebDriver driver, WebElement element, String attributeName) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].removeAttribute(arguments[1],arguments[2])", element,attributeName);
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
} @AfterMethod
public void afterMethod() {
//driver.quit();
}
}

WebDriver高级应用实例(1)的更多相关文章

  1. WebDriver高级应用实例(10)

    10.1控制HTML5语言实现的视频播放器 目的:能够获取html5语言实现的视频播放器视频文件的地址.时长.控制进行播放暂停 被测网页的网址: http://www.w3school.com.cn/ ...

  2. WebDriver高级应用实例(9)

    9.1封装操作表格的公用类 目的:能够使自己编写操作表格的公用类,并基于公用类进行表格中的元素的各类操作 被测网页的网址的HTML代码: <html> <body> <t ...

  3. WebDriver高级应用实例(8)

    8.1使用Log4j在测试过程中打印日志 目的:在测试过程中,使用Log4j打印日志,用于监控和后续调试测试脚本 被测网页的网址: http://www.baidu.com 环境准备: (1)访问ht ...

  4. WebDriver高级应用实例(7)

    7.1在测试中断言失败的步骤进行屏幕截图 目的:在测试过程中,在断言语句执行失败时,对当前的浏览器进行截屏,并在磁盘上新建一个yyyy-mm-dd格式的目录,并在断言失败时新建一个已hh-mm-ss格 ...

  5. WebDriver高级应用实例(6)

    6.1精确比较网页截图图片 目的:对于核心界面进行截屏,并且使用测试过程中的截图和以前测试过程中的截图进行比较.确认页面是否发生了改变 被测网页的网址: http://www.baidu.com Ja ...

  6. WebDriver高级应用实例(5)

    5.1对象库(UI Map) 目的:能够使用配置文件存储被测试页面上的元素的定位方式和定位表达式,做到定位数据和程序的分离.方便不具备编码能力的测试人员进行修改和配置. 被测网页的网址: http:/ ...

  7. WebDriver高级应用实例(4)

    4.1操作web页面的滚动条 被测网页的网址: http://v.sogou.com Java语言版本的API实例代码 import org.testng.annotations.Test; impo ...

  8. WebDriver高级应用实例(3)

    3.1自动化下载某个文件 被测网页的网址: https://pypi.org/project/selenium/#files Java语言版本的API实例代码 import java.util.Has ...

  9. WebDriver高级应用实例(2)

    2.1在日期选择器上进行日期选择 被测网页的网址: https://www.html5tricks.com/demo/Kalendae/index.html Java语言版本的API实例代码 impo ...

随机推荐

  1. 多网卡的7种bond模式原理 For Linux

    多网卡的7种bond模式原理 Linux 多网卡绑定 网卡绑定mode共有七种(0~6) bond0.bond1.bond2.bond3.bond4.bond5.bond6 常用的有三种 mode=0 ...

  2. 2018.09.15 bzoj1977:次小生成树 Tree(次小生成树+树剖)

    传送门 一道比较综合的好题. 由于是求严格的次小生成树. 我们需要维护一条路径上的最小值和次小值. 其中最小值和次小值不能相同. 由于不喜欢倍增我选择了用树链剖分维护. 代码: #include< ...

  3. 关于调用Feign client超时得不到结果的问题

    需要在调用方的配置文件加入以下配置 hystrix.command.default.execution.timeout.enabled: false ribbon: ConnectTimeout: R ...

  4. HDU 2561 第二小整数 (排序)

    题意:中文题. 析:输入后,排一下序就好. 代码如下: #include <iostream> #include <cstdio> #include <algorithm ...

  5. Ansible 笔记 (3) - 编写 playbook

    playbook 相当于多个命令的编排组合然后一起运行,类似写脚本.在学习 playbook 之前需要了解 yaml 格式. 编写playbook的步骤: 定义主机与用户 编写任务列表 执行 play ...

  6. Apache Struts 2 Documentation Big Picture

    http://struts.apache.org/docs/big-picture.html 1. HttpServletRequest 穿越各个过滤器到达FilterDispatcher(这个已经不 ...

  7. event对象的clientX,offsetX,screenX,pageX和offsetTop,offsetHeight等等

    先总结下区别: event.clientX.event.clientY 鼠标相对于浏览器窗口可视区域的X,Y坐标(窗口坐标),可视区域不包括工具栏和滚动条.IE事件和标准事件都定义了这2个属性 eve ...

  8. C语言printf的格式

    例1 int a = 12345;printf("%6d",a); // 输出6位不够左边补空格printf("%.6d",a); // 输出6位不够左边补0例 ...

  9. AtCoder - 4351 Median of Medians(二分+线段树求顺序对)

    D - Median of Medians Time limit : 2sec / Memory limit : 1024MB Score : 700 pointsProblem Statement ...

  10. ACL登陆认证

    前篇文章ACL授权实例介绍了授权,授权完成之后,就要进行认证.ACL的认证主要分为登陆认证与即时认证.所谓登录认证就是在用户登陆的时候,进行信息认证.根据用户Id,加载上来该用户所拥有的权限模块:而即 ...