Selenium+Webdriver学习(三) 执行JS脚本
来源:互联网 发布:同盾大数据 编辑:程序博客网 时间:2024/05/22 11:48
在用selenium 1.X的时候常常会用到getEval()方法来执行一段js脚本来对页面进行处理,以处理一些遇到的问题。当然selenium webdriver也提供这样的一个方法:executeScript()
下面演示在打开51.com首页如何得到帐号输入框中显示的字符,并打印输出。
- /*** This class is about Execute javascript
- * @author annie.wang
- */
- package com.annie.test;
- import org.openqa.selenium.JavascriptExecutor;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.firefox.FirefoxDriver;
- public class Execu_JS {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- WebDriver driver=new FirefoxDriver();
- driver.get("http://www.51.com");
- /** Selenium 执行JS*/
- String js = "var user_input = document.getElementById(\"passport_51_user\").title;return user_input;";
- String input_title=(String)((JavascriptExecutor)driver).executeScript(js);
- System.out.println("---input title---"+input_title);
- String title = driver.getTitle();
- //得到当前页面url
- String currentUrl = driver.getCurrentUrl();
- //输出title和currenturl
- System.out.println("页面title:"+title+"\n"+"当前URL"+currentUrl);
- // ((JavascriptExecutor)driver).executeScript("alert(\"hello,this is a alert!\")");
- String windowHandle=driver.getWindowHandle() ; //返回当前浏览器的窗口句柄
- System.out.println("当前窗口句柄:"+windowHandle+"\n"+windowHandle);
- //String windowHandles=driver.getWindowHandles(Set<STRING>); // 返回所有浏览器的窗口句柄
- // String pageSource=driver.getPageSource() ; //返回当前页面的源码
- // System.out.println(pageSource+"\n"+pageSource);
- }
- }
- </STRING>
/*** This class is about Execute javascript* @author annie.wang */package com.annie.test;import org.openqa.selenium.JavascriptExecutor;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;public class Execu_JS { public static void main(String[] args) {// TODO Auto-generated method stubWebDriver driver=new FirefoxDriver();driver.get("http://www.51.com");/** Selenium 执行JS*/ String js = "var user_input = document.getElementById(\"passport_51_user\").title;return user_input;"; String input_title=(String)((JavascriptExecutor)driver).executeScript(js); System.out.println("---input title---"+input_title); String title = driver.getTitle(); //得到当前页面url String currentUrl = driver.getCurrentUrl(); //输出title和currenturl System.out.println("页面title:"+title+"\n"+"当前URL"+currentUrl); // ((JavascriptExecutor)driver).executeScript("alert(\"hello,this is a alert!\")"); String windowHandle=driver.getWindowHandle() ; //返回当前浏览器的窗口句柄 System.out.println("当前窗口句柄:"+windowHandle+"\n"+windowHandle); //String windowHandles=driver.getWindowHandles(Set); // 返回所有浏览器的窗口句柄 // String pageSource=driver.getPageSource() ; //返回当前页面的源码 // System.out.println(pageSource+"\n"+pageSource); }}
结果如下:
0 0
- Selenium+Webdriver学习(三) 执行JS脚本
- Selenium+Webdriver学习(三) 执行JS脚本
- selenium webdriver学习(三)------------执行js脚本
- Selenium-webdriver系列教程(三)————如何执行一段js脚本
- 执行 Selenium WebDriver 测试脚本抛出 StaleElementReferenceException
- 在selenium(webdriver)中执行js
- Selenium-webdriver系列教程(3)———如何执行一段js脚本
- Eclipse 执行 Selenium WebDriver 测试脚本无法启动浏览器
- webdriver UI自动化测试执行JS脚本
- selenium-webdriver(python) (三)
- selenium Webdriver学习笔记
- Selenium Webdriver 学习
- Selenium + Webdriver 学习(一)
- Selenium webdriver学习历程
- selenium-webdriver 学习记录
- Selenium webDriver学习资源
- selenium webdriver 学习总结
- Selenium webdriver 学习笔记(三) - 基本UI控件操作
- 选择排序 Java
- 排序(一):直接插入排序
- 法被打伤分不记得会变得很加班费吧就
- 每日算法(链表)
- 【原创】C语言处理文件数据(4)
- Selenium+Webdriver学习(三) 执行JS脚本
- 基站使用api说明
- HDU4960 Another OCD Patient
- HBase shell常用命令练习(1)
- 无线路由新改革:触摸更显质感
- Android 抽象布局之include、merge、Viewstub
- 黑马程序员——线程小结
- 谷歌再次降下云计算价格
- Android系统用于Activity的标准Intent