【问题标题】:How to add and execute a JavaScript function to a web page with Watir如何使用 Watir 向网页添加和执行 JavaScript 函数
【发布时间】:2018-06-20 20:09:06
【问题描述】:

如何将 JavaScript 函数添加到给定的网页,执行它,然后使用 Watir 读取其结果?

require 'watir'
myfn = %<
  function samsFunction () {
    var samsBigVariable;
    window.samsBigVariable = "my secret goes here";
  };
  samsFunction();
>.gsub(/\s+/, ' ').strip
b = Watir::Browser.new :firefox
b.goto "google.com"
b.execute_script myfn
b.execute_script "window.samsBigVariable"
 => nil

在控制台中执行相同的 JavaScript 会导致在调用时设置并返回变量。 execute_script 有什么不同?

【问题讨论】:

  • 向那些不加解释地投反对票的人干杯。

标签: javascript ruby selenium-webdriver watir


【解决方案1】:

需要显式返回值:

b.execute_script "return window.samsBigVariable"
#=> "my secret goes here"

【讨论】:

猜你喜欢
  • 2018-06-20
  • 1970-01-01
  • 2012-11-16
  • 2021-11-05
  • 2011-08-28
  • 2019-06-27
  • 2010-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多