eunice
eunice

Reputation: 85

Ruby Watir to get specific html element of a page

I have already try this one:

puts browser.text

and it works. I would like to ask if it is possible to display or print only the specific element or html tags. For example this one:

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

Is there any way to get this element only? Not the whole Page source. I also try

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

and the error is:

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'

Upvotes: 0

Views: 1374

Answers (4)

dalejr
dalejr

Reputation: 1

underlying HTML code for an element on the page can be displayed using method inner_html

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

Upvotes: 0

Smooyk
Smooyk

Reputation: 409

I don't know why Andrew Leaf answer doesn't work for you, it's simple and correct way that should work, could you post an error that it throws? But you can also try this:

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

Upvotes: 3

Sonja Leaf
Sonja Leaf

Reputation: 223

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

That should display the HTML of the element.

Upvotes: 2

Mike S
Mike S

Reputation: 11429

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

Upvotes: 0

Related Questions