【问题标题】:Ruby Watir to get specific html element of a pageRuby Watir 获取页面的特定 html 元素
【发布时间】:2015-02-17 06:11:37
【问题描述】:

这个我已经试过了:

puts browser.text

它有效。我想问是否可以仅显示或打印特定元素或 html 标签。 比如这个:

<button class="btn btn-add" data-role="add">Add New User</button>

有没有办法只获取这个元素?不是整个页面源。 我也试试

puts @browser.button(:class => "btn btn-add").html

错误是:

1) Error:
test_login(TC_Login):
Selenium::WebDriver::Error::JavascriptError: invalid 'in' operand a
    [remote server] http://sample.org/user/ line 68 > Function:18:in `G'
    [remote server] http://sample.org/user/ line 68 > Function:18:in `anonymous/</<'
    [remote server] http://sample.org/user/ line 68 > Function:15:in `anonymous/<'
    [remote server] http://sample.org/user/ line 68 > Function:15:in `anonymous'
    [remote server] http://sample.org/user/:68:in `handleEvaluateEvent'
    c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/remote/response.rb:52:in `assert_ok'
    c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/remote/response.rb:15:in `initialize'
    c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/remote/http/common.rb:59:in `new'
    c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/remote/http/common.rb:59:in `create_response'
    c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/remote/http/default.rb:66:in `request'
    c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/remote/http/common.rb:40:in `call'
    c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/remote/bridge.rb:640:in `raw_execute'
    c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/remote/bridge.rb:618:in `execute'
    c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/remote/bridge.rb:339:in `executeScript'
    c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/common/driver.rb:193:in `execute_script'
    c:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.11/lib/watir-webdriver/atoms.rb:20:in `execute_atom'
    c:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.11/lib/watir-webdriver/elements/element.rb:290:in `outer_html'
    excel.rb:277:in `test_login'

【问题讨论】:

    标签: ruby watir watir-webdriver


    【解决方案1】:

    我不知道为什么 Andrew Leaf 的答案对你不起作用,它应该是简单而正确的方法,你能发布一个它抛出的错误吗? 但你也可以试试这个:

    browser.execute_script('return document.getElementsByClassName("btn-add")[0];').html
    

    【讨论】:

    • 你使用什么浏览器/版本?
    • 我使用的是 firefox 35.0.1
    • 那么我建议你: 1. 尝试更新你的宝石; 2. 将您的 Firefox 降级到版本 34,因为 Firefox 35.x 中存在一些问题,在 selenium-webdriver 中检测到。
    • 是否可以只降级您要使用的浏览器。不是默认的?
    • forums.mozillazine.org/viewtopic.php?f=23&t=2249039 - 阅读这篇文章,它将帮助您安装两个版本的 Firefox。然后更改您的 PATH 变量,使其包含到 firefox 34.x 的路径,并且不包含到 firefox 35.x 的路径。现在你的 webdriver 应该使用 34.x 版本,测试你的代码。将您的 PATH 变量更改为初始条件。附:如果您使用控制台测试代码,请不要忘记在每次 PATH 变量更改后重新启动控制台。
    【解决方案2】:
    @browser.button(:class => "btn btn-add").html
    

    这应该显示元素的 HTML。

    【讨论】:

      【解决方案3】:
      browser.button(:class => "btn btn-add")
      

      【讨论】:

      • 嗨。啊..谢谢您的回复..我想添加一个问题。不能显示html元素???我尝试 puts @browser.button(:class=> "btn btn-add").text.. 但它没有显示出来。
      【解决方案4】:

      可以使用方法 inner_html 显示页面上元素的底层 HTML 代码

      puts browser.button(:class=> "btn btn-add").inner_html

      【讨论】:

        猜你喜欢
        • 2018-12-09
        • 2018-07-06
        • 1970-01-01
        • 2019-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多