1

I'm working with HTML code in ruby and am trying to retrieve a value from a table. Here's what the HTML code looks like

<table class="forumline" border="0" width="90%">

<tr>

<td align="center" class="row2" width="15%">Number<br><input type="text" name="T12" size="20" value="33.5756"></td>

</tr>
</table>

I got it to where my program successfully finds the name but how would I get the value belonging to it which is '33.5756'

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
  driver.get("---site goes here---")
  table_data = driver.find_element(:name, "T13")
5
  • can i get your table name and atributes Commented Jan 31, 2014 at 5:30
  • hm ok its been a long time since I dealt with Html code but the table doesn't have a name to it... Im double checking Commented Jan 31, 2014 at 5:35
  • 1
    Try table_data.attribute("value") or get_value(:name => "T13") Commented Jan 31, 2014 at 5:37
  • 2
    driver = Selenium::WebDriver.for :firefox driver.get("---site goes here---") table_data = driver.find_element(:name, "T13") Have you tried the ............. (:name,"T12") Commented Jan 31, 2014 at 5:39
  • wow that simple I keep believe that worked been looking for hours. God bless you and your family thank you so much Commented Jan 31, 2014 at 5:39

1 Answer 1

1

Write code as below :

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
driver.get("---site goes here---")
table_data = driver.find_element(:name, "T12")
val = table_data.attribute('value')
puts val # => '33.5756'

The method documentation is as below :

(String?) attribute(name) :

Selenium::WebDriver::Element#attribute

Get the value of a the given attribute of the element. Will return the current value, even if this has been modified after the page has been loaded. More exactly, this method will return the value of the given attribute, unless that attribute is not present, in which case the value of the property with the same name is returned. If neither value is set, nil is returned. The “style” attribute is converted as best can be to a text representation with a trailing semi-colon. The following are deemed to be “boolean” attributes, and will return either “true” or “false”:

async, autofocus, autoplay, checked, compact, complete, controls, declare, defaultchecked, defaultselected, defer, disabled, draggable, ended, formnovalidate, hidden, indeterminate, iscontenteditable, ismap, itemscope, loop, multiple, muted, nohref, noresize, noshade, novalidate, nowrap, open, paused, pubdate, readonly, required, reversed, scoped, seamless, seeking, selected, spellcheck, truespeed, willvalidate

Sign up to request clarification or add additional context in comments.

4 Comments

is there a way for the browser not to pop open once the first line of code is called
calling Selenium::WebDriver.for :firefox opens up firefox window automatically and any other type of browser like explorer if you want. Is there a way to use selenium webdriver without having it open a browser
I read that mechanize is only good for submitting, following links and filling out forms, which I have no need for. I looked at the class before I use selenium but i'll double check again. Hopefully they have a similar method to find_element. Thanks for the advice
yeah I see they have form_with to find a form with a specific name in the api i'll look for something to search for a <td> name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.