首先html源文件如下,设置为text 、hidden、submit
view code
在浏览器加载之后如下:
这时候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";
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
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!!
最新内容请见作者的github页:http://qaseven.github.io/