【问题标题】:Issue with execute_scriptexecute_script 的问题
【发布时间】:2013-01-03 16:24:57
【问题描述】:

我正在使用以下配置运行 watir-classic 3.3.0:

  • 红宝石 1.9.2p290
  • 经典 3.3.0
  • Windows XP Service Pack 3
  • IE 8

当我尝试在我正在测试的其中一个页面上执行以下脚本时,我收到一个错误

@browser.execute_script "window.confirm = function() { return true; }"

错误:

WIN32OLERuntimeError: (in OLE method `execScript': )
OLE error code:80020101 in <Unknown>
  Could not complete the operation due to error 80020101.
HRESULT error code:0x80020009
  Exception occurred.
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/page-container.rb:46:in `method_missing'
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/page-container.rb:46:in `rescue in execute_script'
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/page-container.rb:39:in `execute_script'
    from (irb):7
    from C:/Ruby192/bin/irb:12:in `<main>'

当我查看浏览器的 Javascript 错误时,我得到以下信息:

Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET4.0C; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Timestamp: Thu, 3 Jan 2013 16:13:47 UTC


Message: Invalid character
Line: 1
Char: 1
Code: 0
URI: file:///C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/ext/json2.js


Message: 'JSON' is undefined
Line: 1
Char: 1
Code: 0
URI: http://iis01/XXX/employees/default.asp

注意:我在 IE Javascript 错误日志中多次遇到此问题。

该网站没有加载错误,我不知道为什么 JSON2 无法识别为无效字符。关于如何解决这个问题的任何想法?

【问题讨论】:

    标签: ruby watir watir-classic


    【解决方案1】:

    我这里没有 IE8 可以试用,但你能在你的 IE8 上试用吗?

    • 打开 IE8 并转到 about:blank。
    • 打开开发者工具并打开脚本标签
    • 运行此命令:typeof JSON
    • 运行这个命令:typeof JSON.stringify
    • 结果如何?

    如果其中任何一个未定义或您收到错误,则 Watir 会尝试在 PageContainer#with_json2_if_needed 中像这样动态加载 json2.js:

    if (!window.JSON || !window.JSON.stringify) {
      var json2=document.createElement('script');
      json2.type='text/javascript';
      json2.src='file:///C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/ext/json2.js'; 
      document.getElementsByTagName('head')[0].appendChild(json2)
    }
    

    您能否尝试从开发人员工具手动运行该代码时会发生什么?

    如果成功,那么也尝试运行 JSON.stringify:

    JSON.stringify({value: (function() {window.confirm = function() { return true; }})()});
    

    【讨论】:

    • 嘿 Jarmo,我在 Windows 8 的开发工具中输入了命令,我得到 Invalid character json2.js, line1 character 1。这可能是编码问题吗?
    • 这些 typeof 命令输出了什么?不确定编码。你能尝试将json2.js的所有内容复制到开发者工具中,然后看看你会得到什么错误。如果您没有收到任何错误,请尝试执行该 JSON.stringify 代码。
    【解决方案2】:

    我尝试了很多方法来解决这个问题,比如修改许多 IE(在我的例子中是 10 版)安全设置。一般来说,关闭“保护模式”是可行的,但 Watir 的其余部分不起作用(??)。无论如何,使用 json2.js 的 CDN 版本是可行的。这是猴子补丁(我放入 spec_helper.rb)。

    module Watir
      module PageContainer
    
        private
    
        def with_json2_if_needed source
          %Q[
      (function() {
        if (!window.JSON || !window.JSON.stringify) {
          var json2=document.createElement('script');
          json2.type='text/javascript';
          json2.src='https://cdnjs.cloudflare.com/ajax/libs/json2/20150503/json2.js';
          document.getElementsByTagName('head')[0].appendChild(json2)
        }
        return JSON.stringify({value: (function() {#{source}})()});
      })()
          ]
        end
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-24
      • 1970-01-01
      • 2021-03-07
      • 1970-01-01
      • 2015-10-02
      • 2021-09-09
      相关资源
      最近更新 更多