基于Selenium2+Java的UI自动化(5) - 执行JavaScript脚本
一、操作日期选择框
说明:日期选择框大部分是不支持前端输入的,因为这个对象是 readOnly,只读属性,selenium提供了JavaScript执行器,对前台源代码进行操作,间接达到输入传值的目的;
(1)方式一:删除对象的readOnly属性,然后对输入框输入操作;
/**
* 去掉日期选择框的只读属性:readOnly,然后对日期输入框输入值;
*/
public void selectDate01(){
//获取第一个日期选择框元素对象;
WebElement startTime = driver.findElement(By.id("txtStartDate"));
//获取第二个日期选择框元素对象;
WebElement endTime = driver.findElement(By.id("txtEndDate"));
//声明一个Js 执行器对象;
JavascriptExecutor js = (JavascriptExecutor) driver;
/**
* 目的:对两个日期输入框输入日期;
*/
js.executeScript("arguments[0].removeAttribute('readOnly');"
+ "arguments[1].removeAttribute('readOnly');", startTime,endTime);
//arguments[0] 代表第一个元素对象:startTime
//arguments[1] 代表第二个元素对象:endTime 、以此类推,元素个数没有限制,下标从0开始;
//去掉两个对象的只读属性之后,就可以对输入框进行文本输入了;
startTime.sendKeys("2016-11-18");
endTime.sendKeys("2016-12-18");
}
(2)方式二:直接日期输入框赋值value
/**
* 直接对日期输入框赋值:value
*/
public void selectDate03(){
//获取第一个日期选择框元素对象;
WebElement startTime = driver.findElement(By.id("txtStartDate"));
//获取第二个日期选择框元素对象;
WebElement endTime = driver.findElement(By.id("txtEndDate"));
//声明一个Js 执行器对象;
JavascriptExecutor js = (JavascriptExecutor) driver;
/**
* 目的:对两个日期输入框输入日期;
*/
js.executeScript("arguments[0].value='2016-11-18';"
+ "arguments[1].value='2016-12-18';", startTime,endTime);
}
二、对链接设置本页打开
说明:有的时候,我们点击一个链接,发现竟然打开了一个新窗口,这样就会涉及到多个页面来回切换,要使用到句柄的概念(后边在详细讲解: http://hordehome.com/t/selenium2-java-ui-17/1272 ),比较麻烦,而且容易出错。
原因:这个链接元素,有一个属性是target,当target值是 "blank" 时,是在新窗口打开,当target 值是 "self" 或者 没有target属性时,是在本窗口加载新页面,不打开新窗口。
所以,这就需要用到js执行器,对target属性进行操作。
(1)方式一:设置链接元素的target属性值为_self
/**
* 更改target值为_self
*/
public void operatorTarget01(){
//获取链接元素对象;
WebElement link = driver.findElement(By.xpath("//a[text()='修改']"));
//声明一个Js 执行器对象;
JavascriptExecutor js = (JavascriptExecutor) driver;
/**
* 目的:更改链接元素的 target 属性为 _self;
*/
js.executeScript("arguments[0].setAttribute('target','_self');",link);
//点击链接元素
link.click();
}
(2)方式二:删除链接元素的target属性
/**
* 删除链接元素的target 属性
*/
public void operatorTarget03(){
//获取链接元素对象;
WebElement link = driver.findElement(By.xpath("//a[text()='修改']"));
//声明一个Js 执行器对象;
JavascriptExecutor js = (JavascriptExecutor) driver;
/**
* 目的:删除链接元素的 target 属性;
*/
js.executeScript("arguments[0].removeAttribute('target');",link);
//点击链接元素
link.click();
}
基于Selenium2+Java的UI自动化(5) - 执行JavaScript脚本的更多相关文章
- 基于Selenium2+Java的UI自动化(4) - WebDriver API简单介绍
1. 启动浏览器 前边有详细介绍启动三种浏览器的方式(IE.Chrome.Firefox): private WebDriver driver = null; private String chrom ...
- 基于Selenium2+Java的UI自动化(1) - 原理和环境搭建
一.Selenium2的原理 Selenium1是thoughtworks公司的一个产品经理,为了解决重复烦躁的验收工作,写的一个自动化测试工具,其原理是用JS注入的方 式来模拟人工的操作,但是由于J ...
- 基于Selenium2+Java的UI自动化(8)- 显式等待和隐式等待
一.隐式等待 package com.automation.waits; import java.util.concurrent.TimeUnit; import org.openqa.seleniu ...
- 基于Selenium2+Java的UI自动化(6)-操作Alert、confirm、prompt弹出框
alert.confirm.prompt这样的js对话框在selenium1 时代处理起来比价麻烦,常常要用autoit来帮助处理.而现在webdriver对这些弹出框做了专门的处理,使用seleni ...
- 基于Selenium2+Java的UI自动化(2) - 启动浏览器
一.准备工作 我们常用的浏览器主要有三个:chrome.Firefox.IE:其中chrome 和 IE 需要下载驱动程序,才能启动浏览器,注意驱动程序有32位和64位两种. 另外:如何查看本机的浏览 ...
- 基于Selenium2+Java的UI自动化(3) - 页面元素定位
一.几种网页定位方式 webdriver的页面定位很灵活,提供了8种定位方式: 其中,常见的有三种:id .cssSelector .xpath: 一个元素如果存在 id 属性,则这个 id 的值,在 ...
- Java执行JavaScript脚本破解encodeInp()加密
一:背景 在模拟登录某网站时遇到了用户名和密码被JS进行加密提交的问题,如图: 二:解决方法 1.我们首先需要获得该JS加密函数,一般如下: conwork.js var keyStr = " ...
- 在WebBrowser中执行javascript脚本的几种方法整理(execScript/InvokeScript/NavigateScript) 附完整源码
[实例简介] 涵盖了几种常用的 webBrowser执行javascript的方法,详见示例截图以及代码 [实例截图] [核心代码] execScript方式: 1 2 3 4 5 6 7 8 9 1 ...
- Selenium执行JavaScript脚本
JavaScript是运行在客户端(浏览器)和服务器端的脚本语言,允许将静态网页转换为交互式网页.可以通过 Python Selenium WebDriver 执行 JavaScript 语句,在We ...
随机推荐
- UVALive 7454 Parentheses (栈+模拟)
Parentheses 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/A Description http://7xjob4.c ...
- when not exists 用法
USE [ChangHong_612]GO/****** Object: StoredProcedure [dbo].[st_MES_UpdateInspectResult] Script Date: ...
- IOS学习网址
iOS定位和位置信息获取 http://www.cnblogs.com/496668219long/p/4471757.html iOS开发系列--并行开发其实很容易 http://www.cnblo ...
- Usage of readonly and const
Many new learners can not make sure the usage scenarios of readonly and const keywords. In my opinio ...
- Python requests模块
import requests 下面就可以使用神奇的requests模块了! 1.向网页发送数据 >>> payload = {'key1': 'value1', 'key2': [ ...
- C语言根据日期取其位于一年中的第几天
#include <iostream> #include <stdlib.h> using namespace std; bool isLeapYear( int iYear ...
- 使用C# 实现文件锁
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 自己动手写CPU之第九阶段(8)——MIPS32中的LL、SC指令说明
将陆续上传新书<自己动手写CPU>,今天是第47篇. 9.7 ll.sc指令实现思路 9.7.1 实现思路 这2条指令都涉及到訪问链接状态位LLbit,能够将LLbit当做寄存器处理,ll ...
- cocos2d-x 资源路径研究
调用cc.FileUtils:getInstance():addSearchResolutionsOrder("src"); 加入�一个搜索路径,就能够直接载入src文件夹下的资源 ...
- Servlet---JavaWeb技术的核心基础,JavaWeb框架的基石(一)
初学JavaWeb开发,请远离各种框架,从Servlet开始. Web框架是开发者在使用某种语言编写Web应用服务端是关于架构的最佳实践.很多Web框架是从实际的Web项目抽取出来的, ...