I need to check if a certain column is displayed in the table. Each column has a TH header with an unique ID. What would the best way to check for the existence on a TH element in a table?

Here is an example of the table code.

<table id="attr-table">
  <thead>
    <tr>
      <th id="attr-action-col"><input type="checkbox" value="" class="attr-action-box" id="attr-action-col_box"></th>
      <th id="attr-name-col">Name<span class="ui-icon ui-icon-triangle-1-n"></span></th>
      <th id="attr-type-col"></th>
      <th id="attr-scope-col"></th>
      <th id="attr-value-col">English Value</th>
      <th id="attr-master-col">Master Value</th>
      <th id="attr-translation-col">T</th>
      <th id="attr-workflow-col">Status</th>
      <th id="attr-history-col">H</th>
    </tr>
  </thead>
<tbody>
....
</tbody></table>

TIA

有帮助吗?

解决方案

Try th, it should work in watir-webdriver, if it does not work in other Watir gems:

browser.th(:id, 'attr-translation-col').exists?

其他提示

generally the .exists? method is your best bet to see if a given element exists. it returns true or false..

browser.cell(:id, 'attr-translation-col').exists?

=-=-=-= Edit to reflect stuff learned via comments and some resulting investigation =-=-=-=

the .cell method is implemented very differently in Watir vs Watir-Webdriver. This does not appear to be included in the doc about the differences between the two.

In Watir, the .cell method is defined in the Watir::Container module and returns a tablecell object. This makes the .cell method available nearly anywhere from anything that inherets from Container or includes it. For example the IE object. The tablecell object is inhereted from the Element class, and thus has access to most of the expected methods such as .exists?

In Watir-Webdriver, the .cell method is defined in Watir::CellContainer. That module is included in Watir::TableRow, which means that (presuming I am understanding this correctly) the .cell method is only available from within a TableRow object, or something that includes TableRow or inherits from it. For example if you review a Watir-Webdriver Browser object, you won't see the .cell method listed. Like Watir, the tablecell object is inhereted from the Element class, and likewise should have access to the .exists? method.

In contrast to .cell, the Watir-Webdriver methods for .tr, .td, .th are all defined in Watir::Container (as opposed to CellContainer) and thus are highly available and will usually work.

So what this analysis of the Rdoc's for the two projects is telling me is that if you can actually get your hands on a tablecell object in Watir-webdriver, you should be able to call the .exists? method, however you might find the number of places you can use .cell to get a tablecell highly restricted since few objects support this method. If you are seeing an error about a method not existing, it may well be the .cell method, NOT the .exists? method (where Watir-Webdriver is concerned)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top