如何根据行中的文本单击表中的链接
使用page-object和watir-webdriver如何根据行文本单击表中的链接,如下所示:
该表包含3行,其中第一列中包含名称,右侧列中包含相应的“详细信息”链接:
仪表板….详情
示例……细节
等等。
ALL THE DETAILS: ....soon
....some element Details ---> I should able click this link
您可以找到包含指定文本的单元格,转到父行,然后在该行中查找详细信息链接。
假设您可能希望单击其他详细信息链接,我将定义一个view_details方法,该方法接受您要查找的行的文本:
class MyPage include PageObject table(:grid){ div_element(:class => 'basicGridWrapper') .table_element(:class => 'basicGridTable') } def view_details(label) grid_element.cell_element(:text => /#{label}/) .parent .link_element(:text => 'Details') .click end end
然后,您可以单击以下链接:
page.view_details('DASHBOARD')
表元素包括Enumerable模块,我觉得它在这些情况下非常有用。 http://ruby-doc.org/core-2.0.0/Enumerable.html 。 您可以使用find方法查找并返回与您要查找的条件匹配的行。 例如:
class MyPage include PageObject table(:grid_table, :class => 'basicGridTable') def click_link_by_row_text(text_value) matched_row = locate_row_by_text(text_value) matched_row.link_element.click #if you want to make sure you click on the link under the 3rd column you can also do this... #matched_row[2].link_element.click end def locate_row_by_text(text_value) #find the row that matches the text you are looking for matched_row = grid_table_element.find { |row| row.text.include? text_value } fail "Could not locate the row with value #{text_value}" if matched_row.nil? matched_row end end
这里,locate_row_by_text将查找包含您要查找的文本的行,如果找不到它将抛出exception。 然后,一旦找到该行,您可以深入查看该链接,然后单击它,如click_link_by_row_text方法所示。
仅为后代,我想提供一个更新的答案。 现在可以使用table_element[row_index][column_index]
遍历表。 更冗长一点:
-
row_index
也可以是要匹配的行中的文本 – 在您的情况下 –table_element['DASHBOARD']
-
然后使用索引(从零开始)或该列的标题找到相应的cell / td元素
table_element['DASHBOARD'][2]
– 选择所选行中的第三个元素。
由于您没有标题行(
元素),因此可以使用链接的class属性过滤单元格元素。 像这样的东西
table_element['DASHBOARD'].link_element(:class => 'bicRowFac10').click
所以代码看起来像这样:
class MyPage include PageObject def click_link_by_row_text(text_value) table_element[text_value].link_element(:class => 'bicRowFac10').click end end