selenium web driver 使用JS修改input属性
selenium获取input时候,发现type=”hidden” 的input无法修改value,经牛人指点,可以使用js修改
首先html源文件如下,设置为text 、hidden、submit
<html>
<head>
<title>this is a test </title>
<script type="text/javascript">
function display_alert()
{
alert("I am an alert box!!")
}
</script>
</head>
<body>
<form action="form_action.jsp" method="get">
<p>Name: <input type="text" id="fn" name="fullname" /></p>
<p>Email: <input type="hidden" id="em" name="email" value="dbyl@dbyl.cn"/></p>
<p><input type="submit" id="su" onclick="display_alert()" value="submit" /></p> </form>
</body>
</html>
在浏览器加载之后如下:

这时候email 不能对外显示
使用selenium,代码如下
import org.openqa.selenium.Alert;
import org.openqa.selenium.JavascriptExecutor;
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.ie.InternetExplorerDriver;
//import org.openqa.selenium.remote.DesiredCapabilities; public class selenium { /**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub String URL="E:\\2.html";
//set web driver property
System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
//create a WebDriver instance
WebDriver driver = new ChromeDriver() ;
driver.manage().window().maximize(); //load the URL
driver.get(URL);
//print current title
System.out.println(driver.getTitle());
//run JS to modify hidden element
((JavascriptExecutor)driver).executeScript("document.getElementById(\"em\").type ='text';");
Thread.sleep(3000);
//run JS and add a alert
((JavascriptExecutor)driver).executeScript("alert(\"hello,this is a alert!\");value=\"Alert\""); //wait for 3 seconds
Thread.sleep(3000); //create a alert instance
Alert alert1=driver.switchTo().alert();
//print alert text
System.out.println(alert1.getText());
//click accept button
alert1.accept(); //create elements
WebElement we=driver.findElement(By.id("fn"));
WebElement su=driver.findElement(By.id("su"));
WebElement em=driver.findElement(By.id("em"));
// input something
we.sendKeys("username test");
Thread.sleep(3000);
//print email tagname
System.out.print("Email isDislayed="+em.isDisplayed()+"\n");
Thread.sleep(3000);
//click submit button
su.click();
Thread.sleep(3000); Alert alert=driver.switchTo().alert();
System.out.print( alert.getText());
alert.accept(); Thread.sleep(3000); //close web browser
driver.quit(); } }
可以通过js修改input的type value,执行js只需要export
import org.openqa.selenium.JavascriptExecutor;
运行结果如下:
Starting ChromeDriver (v2.9.248315) on port 30175
this is a test
hello,this is a alert!
Email isDislayed=true
I am an alert box!!
selenium web driver 使用JS修改input属性的更多相关文章
- js修改input的type属性问题
js修改input的type属性有些限制.当input元素还未插入文档流之前,是可以修改它的值的,在ie和ff下都没问题.但如果input已经存在于页面,其type属性在ie下就成了只读属性了,不可以 ...
- 25+ Useful Selenium Web driver Code Snippets For GUI Testing Automation
本文总结了使用Selenium Web driver 做页面自动化测试的一些 tips, tricks, snippets. 1. Chrome Driver 如何安装 extensions 两种方式 ...
- 修改input属性placeholder的样式
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- selenium web driver 配合使用testng
首先为eclipse添加testng插件 步骤如下:help->Install New SoftWare... 2. 添加testng链接,该链接可以在这里找到 For the Eclipse ...
- selenium web driver 实现截图功能
在验证某些关键步骤时,需要截个图来记录一下当时的情况 Webdriver截图时,需要引入 import java.io.File; import java.io.IOException; import ...
- 使用原生JS 修改 DIV 属性
本例参考并改进自:https://www.jianshu.com/p/2961d9c317a3 大家可以一起学习!! <!DOCTYPE html> <html lang=" ...
- js修改input的type属性问题(兼容所有浏览器,主要用于密码类的默认有提示文字的效果)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- Js修改input值后怎么同步修改绑定的v-model值
v-model只是一种语法糖,底层的方法还是去监听input事件.所以可以使用dispatchEvent事件给元素分配一个input事件,这样可以手动触发 inputElement 的 input 事 ...
- 自动化测试-19.selenium定位之通过JS修改html写入日期数据以及从文本读取数据实战
# -*- coding: utf-8 -*- from selenium import webdriver from selenium.webdriver.support.select import ...
随机推荐
- .Net mvc 根据前台参数动态绑定对象
业务需求:根据前台界面的参数,动态绑定对象 <param name="colNames">属性名拼接字符串</param><param name=&q ...
- java 导出word 并下载
记录一下导出操作 源码: /************ * 导出word 并下载 * @param id 房号记录编号 * ***********************/ @RequestMappin ...
- thinkphp-二次开发1
Thinkphp是一个国人使用的比较多的一个框架 ,具体的也不多了,不知道的可以去百度一下. 现在我们要讲的是如何利用现有的半成品的系统实现二次开发 讲到thinkphp的二次开发不得不说他的兄弟on ...
- neo4j关闭和开启密码访问权限
本例:neo4j-enterprise-2.3.1版本 neo4j默认安装是开启访问密码验证 可以发现,在conf/下的neo4j-server.properties配置文件 # Require (o ...
- js中类型识别的方法
第一种方法typeof typeof是一种运算符,它的值有以下几种 <!DOCTYPE html> <html lang="en"> <head> ...
- 关于input的file框onchange事件触发一次失效的新的解决方法
在google了众多方法后,网上有这么几种方法: 1.替换掉原来的input框 2.remove原来的input框,然后在添加进新的一样的input框 但是不知道为什么非常不幸的是,怎么弄我都弄不出. ...
- [转载]El Capitan 中 SIP 介绍
这两天大家纷纷将 OS X 系统升级到了 El Capitan,然后发现,一些注入的工具无法使用了,某些系统目录无法使用了,第三方未签名的 kext 无法加载了,问题一堆堆的.这是因为,Mac OS ...
- Linux学习之CentOS(二十)--CentOS6.4下修改MySQL编码方法
但是当我们在试图对数据库中的数据进行备份或者将sql文件导入到我们的数据库时可能就会碰到编码的问题,在windows下安装mysql时我们可以在安装的时候就选择好整个数据库的编码方式(通常设置成utf ...
- ES6箭头函数与展开运算符
箭头函数:省去了关键字function和return: eg: reduce=(a,b)=>a+b;//返回a+b的值 redduce=(a,b)=>{console.log(a);con ...
- php ob_flush 和flush
“ob_flush()和flush()的区别.前者是把数据从PHP的缓冲中释放出来,后者是把不在缓冲中的或者说是被释放出来的数据发送到浏览器.所以当缓冲存在的时候,我们必须ob_flush()和flu ...