在Watir中显示HTML元素时出错

时间:2015-02-18 03:15:16

标签: ruby watir watir-webdriver

我有以下几行代码:

if @browser.text_field(:id => "street").exists?
      puts "street exist"
      puts @browser.text_field(:id => "street").html
    else
      puts "street does not exist"
    end

但是在运行这个之后,我有一个错误:

Run options: 

# Running tests:

street exist
E

Finished tests in 90.183158s, 0.0111 tests/s, 0.0000 assertions/s.

  1) Error:
test_login(TC_Login):
Selenium::WebDriver::Error::JavascriptError: invalid 'in' operand a
    [remote server] http://sam.org/user/ line 68 > Function:18:in `G'
    [remote server] http://sam.org/user/ line 68 > Function:18:in `anonymous/</<'
    [remote server] http://sam.org/user/ line 68 > Function:15:in `anonymous/<'
    [remote server] http://sam.org/user/ line 68 > Function:15:in `anonymous'
    [remote server] http://sam.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:279:in `test_login'

1 tests, 0 assertions, 0 failures, 1 errors, 0 skips

该元素已存在。当我尝试puts @browser.html时,它会显示整个页面源。我的目标是只显示特定的HTML元素。我尝试了上面的代码行。我无法理解错误说的是什么。

2 个答案:

答案 0 :(得分:0)

尝试这个,因为这将特定于元素。

@browser.find_element(:id, "street")

答案 1 :(得分:0)

尝试

puts @browser.text_field(:id => "street").value if @browser.text_field(:id => "street").exists?