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!!

最新文章

  1. Maven:jar 下载相关的问题
  2. [翻译svg教程]svg中矩形元素 rect
  3. Machine Learning
  4. 新年PR交期回写,展望期由14天改为30天,FP_PR2SAP ;转单量改为100W;FP_PR2SAP_MOD_NEW
  5. kendo ui的treeView节点点击事件修改和grid的配置的一点总结
  6. 【服务器】CentOS下部署运行NodeJs Web App
  7. 【Alpha阶段】第八次Scrum例会
  8. css用clearfix清除浮动
  9. Emacs常用命令汇总
  10. html中button的type属性
  11. bochs 2.6安装和使用
  12. 水dp第二天(背包有关)
  13. cookie 就是一些字符串信息
  14. 【kubenetus】kubenetus运维
  15. 关于tensorflow conv2d卷积备忘的一点理解
  16. leetcode — next-permutation
  17. Spring Cloud Eureka简介及原理
  18. 【转】如何向Android模拟器打电话发短信
  19. bootstrap1相关学习文档
  20. Bootstrap CustomBox 弹层

热门文章

  1. Android 解析聊天表情的笔记
  2. 冰冻三尺非一日之寒--web来了
  3. OpenCV图像的全局阈值二值化函数(OTSU)
  4. 日历插件FullCalendar应用:(二)数据增删改
  5. 【bzoj2648】 SJY摆棋子
  6. php artisan常用方法
  7. CentOS7清理yum缓存和释放内存方法
  8. Web系统大规模并发——电商秒杀与抢购
  9. 修改github.com域名解析
  10. CentOS 7.0编译安装Nginx1.6.0+MySQL5.6.19+PHP5.5.14