???????????????js???????????
??????????????????????????alert?????

 

1 import org.openqa.selenium.JavascriptExecutor;
2 import org.openqa.selenium.WebDriver;
3 import org.openqa.selenium.firefox.FirefoxDriver;
4
5 public class JavaScript {
6
7     public static void main(String[] args) {
8         WebDriver driver = new FirefoxDriver();
9 //        ???js????
10 //        1.??driver???????JavascriptExecutor????
11 //        2.????executeScript????
12         ((JavascriptExecutor)driver).executeScript("alert("hello??this is a alert!")");
13     }
14 }

???????????JS???????е???????????selenium
????????????????JS????return document.title ????????title???????selenium???????????????????- -#??

 

1 import org.openqa.selenium.WebDriver;
2 import org.openqa.selenium.firefox.FirefoxDriver;
3 import org.openqa.selenium.JavascriptExecutor;
4
5
6 public class JavaScript2 {
7
8     public static void main(String[] args) {
9         WebDriver driver = new FirefoxDriver();
10         driver.get("http://www.baidu.com/");
11 /*
12  *      ?????????????????
13  *      ?÷???????????org.openqa.selenium.JavascriptExecutor?μ????JavascriptExecutor
14         ??????com.gargoylesoftware.htmlunit.javascript.background.JavaScriptExecutor???????
15         JavaScriptExecutor????
16         ????????Java??s???Сд
17  */
18 //        ????JavaScriptExecutor
19         JavascriptExecutor js = (JavascriptExecutor)driver;
20 //        ???js????????title ?????return
21         String title = (String) js.executeScript("return document.title");
22         System.out.println(title);
23         driver.close();
24     }
25 }