使用 WATIR 访问表 TH 元素
我需要检查表中是否显示特定列。 每列都有一个带有唯一 ID 的 TH 标题。 检查表中 TH 元素是否存在的最佳方法是什么?
这是表代码的示例。
<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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
th
,它应该在 watir-webdriver 中工作,如果它在其他 Watir gems 中不起作用:Try
th
, it should work in watir-webdriver, if it does not work in other Watir gems:一般来说.存在吗?方法是查看给定元素是否存在的最佳选择。它返回 true 或 false..
browser.cell(:id, 'attr-translation-col').exists?
=-=-=-= 编辑以反映通过评论和一些调查结果学到的东西=-=-=-=
.cell 方法在 Watir 与 Watir-Webdriver 中的实现方式非常不同。这似乎没有包含在有关两者之间差异的文档中。
在 Watir 中,.cell 方法在 Watir::Container 模块中定义,并返回一个 tablecell 对象。这使得 .cell 方法几乎可以在任何从 Container 继承或包含它的任何地方使用。例如 IE 对象。 tablecell 对象继承自 Element 类,因此可以访问大多数预期的方法,例如
Watir-Webdriver 中的 .exists? ,.cell 方法在 Watir::CellContainer 中定义。该模块包含在 Watir::TableRow 中,这意味着(假设我正确理解了这一点).cell 方法只能从 TableRow 对象或其他对象中使用包括 TableRow 或继承自它。例如,如果您查看 Watir-Webdriver Browser 对象,您将不会看到列出的 .cell 方法。与 Watir 一样,tablecell 对象是从 Element 类继承的,同样应该可以访问 .exists? 方法。
与 .cell 相比,.tr、.td、.th 的 Watir-Webdriver 方法均在 Watir::Container 中定义(与 CellContainer 相对),因此具有高可用性并且通常可以工作。
那么,对这两个项目的 Rdoc 的分析告诉我的是,如果您实际上可以在 Watir-webdriver 中获得 tablecell 对象,那么您应该能够调用 .exists?方法,但是您可能会发现可以使用 .cell 获取表格单元的位置数量受到严格限制,因为很少有对象支持此方法。如果您看到有关方法不存在的错误,很可能是 .cell 方法,而不是 .exists?方法(涉及 Watir-Webdriver)
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)