WebDriver拾级而上·之十三 调用Java Script

在用selenium 1.X的时候常常会用到geteval_r()方法来执行一段js脚本来对页面进行处理。
然selenium webdriver也提供这样的一个方法: JavascriptExecutor. executeScript(string)
例如:
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;

public class<wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)">SimpleExample {</span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)">public static void main(String[] args) {</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)">ChromeDriver driver = new ChromeDriver();</span> <div style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"> <span style="color:#FF0000; word-wrap:normal; word-break:normal; line-height:21px">driver.executeAsyncScript(</span><span style="color:#1C01FF; word-wrap:normal; word-break:normal; line-height:21px">"arguments[0](); alert('Hello')"</span><span style="color:#FF0000; word-wrap:normal; word-break:normal; line-height:21px">);</span> </div> <div style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"> try {</div> <div style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"> Thread.sleep(3000);</div> <div style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"> } catch (InterruptedException e) {</div> <div style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"> e.printStackTrace();</div> <div style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"> }</div> <div style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"> driver.switchTo().alert().accept();</div> <span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)">}</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)">}</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)">上面是一个最简单的例子,打开一个浏览器,然后弹层一个alert框。注意这里的driver要被强制转换成JavascriptExecutor。</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)">下面演示在打开51.com首页如何得到帐号输入框中显示的字符,并打印输出。</span> <div style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"> <a href="http://photo.blog.sina.com.cn/showpic.html#blogid=6966650401012dxv&amp;url=http://s8.sinaimg.cn/orignal/69666504gc0ab3c7fe647" target="_blank" style="text-decoration:none; color:rgb(106,120,123)"><img src="http://s8.sinaimg.cn/middle/69666504gc0ab3c7fe647&amp;690" width="690" height="37" name="image_operate_68481337755557771" alt="WebDriver拾级而上路之十三&lt;wbr&gt;调用Java&lt;wbr&gt;Script" title="WebDriver拾级而上路之十三&lt;wbr&gt;调用Java&lt;wbr&gt;Script" style="margin:0px; padding:0px; border:0px; list-style:none"></a><br><br> </div> <div style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(202,207,207)"> <br><div>package com.test;</div> <div><br></div> <div>import org.openqa.selenium.JavascriptExecutor;</div> <div>import org.openqa.selenium.WebDriver;</div> <div>import org.openqa.selenium.chrome.ChromeDriver;</div> <div><br></div> <div>public class Test_JsExecutor {</div> <div>public static void main(String[] args) {</div> <div>String url = "http://www.51.com";</div> <div>//打开chrome</div> <div>WebDriver dr = new ChromeDriver();</div> <div>String js = "var user_input = document.getElementByIdx_x_x_x(\"passport_51_user\").title;return user_input;";</div> <div>String title =<span style="color:#FF0000; word-wrap:normal; word-break:normal; line-height:21px">(String)((JavascriptExecutor)dr).executeScript( js);</span> </div> <div>System.out.println(title);</div> <div><br></div> <div><wbr><wbr><wbr><wbr>dr.quit();</wbr></wbr></wbr></wbr></div> <div>}</div> <div>}</div> 输出结果为:用户名/彩虹号/邮箱<br><br> 其他用例:<br><wbr><wbr><wbr>JavascriptExecutor js = (JavascriptExecutor) driver;<br><wbr><wbr><wbr>js.executeScript("(function(){<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>inventoryGridMgr.setTableFieldValue('"+ inventoryId + "','"<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>+ fieldName + "','"+ value + "');<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>)()"<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> </div> </wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值