selenium web driver 使用JS修改input属性
selenium获取input时候,发现type=”hidden” 的input无法修改value,经牛人指点,可以使用js修改
首先html源文件如下,设置为text 、hidden、submit

1 <html> 2 <head> 3 <title>this is a test </title> 4 <script type="text/javascript"> 5 function display_alert() 6 { 7 alert("I am an alert box!!") 8 } 9 </script> 10 </head> 11 <body> 12 <form action="form_action.jsp" method="get"> 13 <p>Name: <input type="text" id="fn" name="fullname" /></p> 14 <p>Email: <input type="hidden" id="em" name="email" value="dbyl@dbyl.cn"/></p> 15 <p><input type="submit" id="su" onclick="display_alert()" value="submit" /></p> 16 17 </form> 18 </body> 19 </html>
在浏览器加载之后如下:
这时候email 不能对外显示
使用selenium,代码如下
1 import org.openqa.selenium.Alert; 2 import org.openqa.selenium.JavascriptExecutor; 3 import org.openqa.selenium.By; 4 import org.openqa.selenium.WebDriver; 5 import org.openqa.selenium.WebElement; 6 import org.openqa.selenium.chrome.ChromeDriver; 7 //import org.openqa.selenium.ie.InternetExplorerDriver; 8 //import org.openqa.selenium.remote.DesiredCapabilities; 9 10 public class selenium { 11 12 /** 13 * @param args 14 * @throws InterruptedException 15 */ 16 public static void main(String[] args) throws InterruptedException { 17 // TODO Auto-generated method stub 18 19 String URL="E:\\2.html"; 20 //set web driver property 21 System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe"); 22 //create a WebDriver instance 23 WebDriver driver = new ChromeDriver() ; 24 driver.manage().window().maximize(); 25 26 //load the URL 27 driver.get(URL); 28 //print current title 29 System.out.println(driver.getTitle()); 30 //run JS to modify hidden element 31 ((JavascriptExecutor)driver).executeScript("document.getElementById(\"em\").type ='text';"); 32 Thread.sleep(3000); 33 //run JS and add a alert 34 ((JavascriptExecutor)driver).executeScript("alert(\"hello,this is a alert!\");value=\"Alert\""); 35 36 //wait for 3 seconds 37 Thread.sleep(3000); 38 39 40 //create a alert instance 41 Alert alert1=driver.switchTo().alert(); 42 //print alert text 43 System.out.println(alert1.getText()); 44 //click accept button 45 alert1.accept(); 46 47 //create elements 48 WebElement we=driver.findElement(By.id("fn")); 49 WebElement su=driver.findElement(By.id("su")); 50 WebElement em=driver.findElement(By.id("em")); 51 // input something 52 we.sendKeys("username test"); 53 Thread.sleep(3000); 54 //print email tagname 55 System.out.print("Email isDislayed="+em.isDisplayed()+"\n"); 56 Thread.sleep(3000); 57 //click submit button 58 su.click(); 59 Thread.sleep(3000); 60 61 Alert alert=driver.switchTo().alert(); 62 System.out.print( alert.getText()); 63 alert.accept(); 64 65 Thread.sleep(3000); 66 67 //close web browser 68 driver.quit(); 69 70 } 71 72 }
可以通过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!!
转载请注明出处:http://www.cnblogs.com/tobecrazy/

软件测试交流QQ群:312937087 we are QA!
分类:
测试
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?