selenium webdriver学习(三)------------执行js脚本
2013-05-30 15:07
369 查看
在用selenium 1.X的时候常常会用到getEval()方法来执行一段js脚本来对页面进行处理,以处理一些遇到的问题。当然selenium webdriver也提供这样的一个方法:executeScript()
Java代码
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
public
class SimpleExample {
public
staticvoid main(String[] args) {
WebDriver driver =
new FirefoxDriver();
((JavascriptExecutor)driver).executeScript("alert(\"hello,this is a alert!\")");
}
}
上面是一个最简单的例子,打开一个浏览器,然后弹层一个alert框。注意这里的driver要被强制转换成JavascriptExecutor。
下面演示在打开51.com首页如何得到帐号输入框中显示的字符,并打印输出。
Java代码
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
public
class FirstExampe {
public
staticvoid main(String[] args) {
WebDriver driver =
new FirefoxDriver();
driver.get("http://www.51.com");
String js = "var user_input = document.getElementById(\"passport_51_user\").title;return user_input;";
String title = (String)((JavascriptExecutor)driver).executeScript( js);
System.out.println(title);
}
}
输出结果为:
Java代码
用户名/彩虹号/邮箱
Java代码
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
public
class SimpleExample {
public
staticvoid main(String[] args) {
WebDriver driver =
new FirefoxDriver();
((JavascriptExecutor)driver).executeScript("alert(\"hello,this is a alert!\")");
}
}
import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; public class SimpleExample { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); ((JavascriptExecutor)driver).executeScript("alert(\"hello,this is a alert!\")"); } }
上面是一个最简单的例子,打开一个浏览器,然后弹层一个alert框。注意这里的driver要被强制转换成JavascriptExecutor。
下面演示在打开51.com首页如何得到帐号输入框中显示的字符,并打印输出。
Java代码
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
public
class FirstExampe {
public
staticvoid main(String[] args) {
WebDriver driver =
new FirefoxDriver();
driver.get("http://www.51.com");
String js = "var user_input = document.getElementById(\"passport_51_user\").title;return user_input;";
String title = (String)((JavascriptExecutor)driver).executeScript( js);
System.out.println(title);
}
}
import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; public class FirstExampe { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("http://www.51.com"); String js = "var user_input = document.getElementById(\"passport_51_user\").title;return user_input;"; String title = (String)((JavascriptExecutor)driver).executeScript( js); System.out.println(title); } }
输出结果为:
Java代码
用户名/彩虹号/邮箱
相关文章推荐
- Selenium+Webdriver学习(三) 执行JS脚本
- Selenium+Webdriver学习(三) 执行JS脚本
- Ruby-selenium-webdriver(一、学习查看文档、ie浏览器简单的执行例子)
- Selenium2.0之WebDriver学习总结(2)
- 自动化测试:Selenium webdriver 学习笔记-C#版(二)
- 自动化测试:Selenium webdriver 学习笔记-C#版(三)
- selenium webdriver 学习总结 - 自定义Table操作类(十一)
- 【学习笔记】Selenium WebDriver的Actions类中的sendKeys方法和WebElement.sendKeys方法的区别
- selenium webdriver 学习总结-JUnit4 入门(三)_补充Annotation
- Selenium + Webdriver 学习(四) 元素定位方法
- selenium webdriver学习(十)------------如何把一个元素拖放到另一个元素里面(转)
- Selenium + Webdriver 学习(一)
- selenium webdriver学习(六)------------如何得到弹出窗口
- 转:selenium webdriver 执行javascript代码
- Selenium2.0之WebDriver学习总结(一)
- selenium(webdriver)学习笔记2--打开指定路径的firefox
- selenium webdriver 学习总结-数据驱动(六)
- 自动化测试:Selenium webdriver 学习笔记-C#版(一)
- 执行 Selenium WebDriver 测试脚本抛出 StaleElementReferenceException
- Selenium2.0之WebDriver学习总结(2)