Get text <TD> using WATIR

I use WATIR for automatic testing, and I need to copy the bid value in the variable. In the example below (from the source code of the webpage) I need the variable myrate has a value of 2.595 . I know how to get the value from <input> or <span> (see below), but not directly from <td> . Any help? Thanks

 <TABLE> <TR> <TD></TD> <TD>Rate</TD> <TD>2.595</TD> </TR> </TABLE> 

For a <span> I use this code:

 raRetrieved = browser.span(:name => 'myForm.raNumber').text 
+4
source share
3 answers

try this, find the line you want, using the regular expression to match the line containing the word "Rate", then get the text of the third cell in the line.

 myrate = browser.tr(:text, /Rate/).td(:index => 2).text #or you can use the more user-friendly aliases for those tags myrate = browser.row(:text, /Rate/).cell(:index => 2).text 

If the word "Rate" can be displayed elsewhere in another textin this table, but is always the only entry in the second cell of the desired row, then find the cell with this exact text, use the parent method to search for the row that contains this cell, and then gets text from the third cell.

 myrate = browser.cell(:text, 'Rate').parent.cell(:index => 2).text 

using .cell and .row vs .td and .tr up to you, some prefer tags, others prefer more descriptive names. Use what you think makes the code the most readable for you or others who will work with it.

Note. The above code assumes the use of Watir-Webdriver or Watir 2.x, which use zero-indexing. For older versions of Watir, change the index values ​​to 3

And for the record, I completely agree with the comments of others that you were unable to verify the sample code that you posted. it's horrible. The request to find the correct elements, such as identification values ​​or names, does not go beyond the scope to facilitate page verification.

+8
source

Try the following:

 browser.td(how, what).text 

The problem here is that the table , tr and td tags have no attributes. You can try something like this (not tested):

 browser.table[0][2].text 
+3
source

If this helps anyone who has the same problem, it works for me as follows:

 browser.td(:text => "Rate").parent.cell(:index, 2).text 

Thank you all

+1
source

Source: https://habr.com/ru/post/1382255/


All Articles