Ruby 如何基于行中的文本单击表中的链接
使用page object和watir webdriver如何根据以下行文本单击表中的链接: 该表包含3行,第一列中有名称,右侧列中有相应的详细信息链接: 仪表板。。。。细节 例如。。。。细节 等等Ruby 如何基于行中的文本单击表中的链接,ruby,selenium-webdriver,watir,watir-webdriver,page-object-gem,Ruby,Selenium Webdriver,Watir,Watir Webdriver,Page Object Gem,使用page object和watir webdriver如何根据以下行文本单击表中的链接: 该表包含3行,第一列中有名称,右侧列中有相应的详细信息链接: 仪表板。。。。细节 例如。。。。细节 等等 <div class="basicGridHeader"> <table class="basicGridTable">ALL THE DETAILS: ....soon </table> </div> <div c
<div class="basicGridHeader">
<table class="basicGridTable">ALL THE DETAILS:
....soon
</table>
</div>
<div class="basicGridWrapper">
<table class="basicGridTable">
<tbody id="bicFac9" class="ide043">
<tr id="id056">
<td class="bicRowFac10">
<span>
<label class="bicDeco5">
<b>DASHBOARD:</b> ---> Based on this text
</label>
</span>
</td>
<td class="bicRowFac11">
....some element
</td>
<td class="bicRowFac12">
<span>
<a class="bicFacDet5">Details</a> ---> I should able click this link
</span>
</td>
</tr>
</tbody>
</table>
</div>
您可以找到包含指定文本的单元格,转到父行,然后在该行中找到详细信息链接 假设您可能希望单击其他详细信息链接,我将定义一个view\u 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')
表元素包括可枚举模块,我发现它在这样的情况下非常有用。您可以使用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将查找包含您正在查找的文本的行,如果找不到,将抛出异常。然后,找到行后,可以向下钻取到链接,并单击它,如单击链接按行文本方法所示 为了子孙后代,我想给出一个最新的答案。现在可以使用table_元素[row_index][column_index]遍历表。 再详细一点: 行索引也可以是要匹配的行中的文本-在您的例子中-表元素['DASHBOARD'] 然后使用基于indexzero的索引或该列的标题查找相应的cell/td元素 表_元素['DASHBOARD'][2]-选择列表中的第三个元素 选定行 由于没有标题行元素,因此可以使用链接的class属性筛选单元格元素。像这样的 表元素['DASHBOARD']。链接元素:class=>bicRowFac10。单击 所以代码看起来像这样: 类MyPage 包含页面对象 def按行单击链接文本值 表元素[文本元素值]。链接元素:class=>bicRowFac10。单击 终止
End到目前为止您尝试了什么?嗨,Justin,谢谢您提供的信息,但是:class=>'bicRowFac10'将动态更改..因此..它无法根据给定的表找到它,您可以跳过类定位器部分。这样做的风险是,它将无法保证要检查的列。如果文本是唯一的,这通常是可以的。Hi Sheg,我尝试过你的代码,但它正在定位文本,脚本正在传递,但没有单击详细信息链接。我使用watir webdriver使用过此代码,它工作正常。我需要在page object中使用相同的方式,以及parent=browser.div:class,“basicGridWrapper”.table.trs.find do | tr | tr.td:text=>/DASHBOARD/.present?end parent.td:index=>2.clickThis给了我一个NoMethodError:on-grid\u table\u元素的未定义方法'find'。找到建议了吗?