有时候在进行自动化测试时需要在页面上执行一段js脚本,这个时候就需要用到execute_script方法了。


require 'selenium-webdriver'
dr = Selenium::WebDriver.for :ff
url = 'http://www.soso.com'
dr.navigate.to url
sleep 3
js = <<JS
    q = document.getElementById("tb");
    q.style.border = "1px solid red";
JS

dr.execute_script js

上面的代码打开了SoSo的首页,并高亮显示了id为”tb”的div。

下面的例子演示了在打开QQ首页的时候如何自动focus到页面上的soso搜索框

require 'rubygems'
require 'selenium-webdriver'
dr = Selenium::WebDriver.for :ff
url = 'http://www.qq.com'
dr.navigate.to url
sleep 3
js = <<JS
    p = document.getElementById("smart_input")
    p.focus()
JS

dr.execute_script js


当dr = Selenium::WebDriver.for :ff换成dr = Selenium::WebDriver.for :ie时,js执行不了,
Selenium-webdriver系列教程(三)————如何执行一段js脚本
这个问题还需要研究,除ie浏览器,别的浏览器都是ok的。


相关文章:

  • 2022-12-23
  • 2021-08-28
  • 2021-05-24
  • 2022-02-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-26
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-08
  • 2021-08-04
  • 2022-12-23
  • 2021-11-25
  • 2021-07-02
相关资源
相似解决方案