如何在表格中逐个单击随机链接

时间:2014-08-30 15:50:12

标签: watir watir-webdriver

我正在自动化一个网站,我想在其中点击表格内的链接。 在UI中,表格显示如下:

行动合同行项目编号产品产品代码
编辑| Del CLI-757042开放式系统(访客中心)SYS010
编辑| Del CLI-757043来自在线GC的客人

我想逐个点击链接CLI-757042,CLI-757043,CLI-757044。这些数字是随机的。

以下是html代码:

<!-- WrappingClass -->
<div class="listRelatedObject customnotabBlock"><div class="bPageBlock brandSecondaryBrd secondaryPalette"><div class="pbHeader"> … </div><div id="800f00000006cQt_00Na000000ASdVK_body" class="pbBody"><table class="list" cellspacing="0" cellpadding="0" border="0"><tbody><tr class="headerRow"> … </tr>
  <!-- ListRow -->
<tr class="dataRow even first" onmouseover="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}"><td class="actionColumn"> … </td><th class=" dataCell  " scope="row"><a href="/a1if0000000ZfEv">
  CLI-757042
</a></th><td class=" dataCell  "><a href="/01ta0000003oV9r"> … </a></td><td class=" dataCell  "> … </td><td class=" dataCell  CurrencyElement"> … </td><td class=" dataCell  "> … </td><td class=" dataCell  CurrencyElement"> … </td><td class=" dataCell  numericalColumn"> … </td><td class=" dataCell  numericalColumn"> … </td><td class=" dataCell  CurrencyElement"> … </td><td class=" dataCell  "> … </td></tr>
  <!-- ListRow -->
<tr class="dataRow odd" onmouseover="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}"><td class="actionColumn"> … </td><th class=" dataCell  " scope="row"><a href="/a1if0000000ZfEw"> … </a></th><td class=" dataCell  "> … </td><td class=" dataCell  "> … </td><td class=" dataCell  CurrencyElement"> … </td><td class=" dataCell  "> … </td><td class=" dataCell  CurrencyElement"> … </td><td class=" dataCell  numericalColumn"> … </td><td class=" dataCell  numericalColumn"> … </td><td class=" dataCell  CurrencyElement"> … </td><td class=" dataCell  "> … </td></tr>
  <!-- ListRow -->
<tr class="dataRow even" onmouseover="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}"> … </tr>
  <!-- ListRow -->
<tr class="dataRow odd" onmouseover="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}"> … </tr>
  <!-- ListRow -->
<tr class="dataRow even last" onmouseover="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}"> … </tr></tbody></table><div class="pShowMore"> … </div></div><div class="pbFooter secondaryPalette"> … </div></div></div><div class="listElementBottomNav"></div><script> … </script>
  <!-- End ListElement -->
</div>
  <!-- End RelatedListElement -->

  <!-- Begin RelatedListElement -->
<div id="800f00000006cQt_00Na000000ASdVM" class="bRelatedList"><a name="800f00000006cQt_00Na000000ASdVM_target"></a>
  <!-- Begin ListElement -->

  <!-- motif: Contract -->

  <!-- WrappingClass -->
<div class="listRelatedObject Custom18Block"><div class="bPageBlock brandSecondaryBrd secondaryPalette"><div class="pbHeader"><table cellspacing="0" cellpadding="0" border="0"><tbody><tr><td class="pbTitle"><img class="minWidth" width="1" height="1" title="" alt="" src="/s.gif"></img><img class="relatedListIcon" title="Order" alt="Order" src="/s.gif"></img><h3 id="800f00000006cQt_00Na000000ASdVM_title"> … </h3></td><td class="pbButton"> … </td><td class="pbHelp"> … </td></tr></tbody></table></div><div id="800f00000006cQt_00Na000000ASdVM_body" class="pbBody"> … </div><div class="pbFooter secondaryPalette"> … </div></div></div><div class="listElementBottomNav"></div><script> … </script>
  <!-- End ListElement -->
</div>
  <!-- End RelatedListElement -->

  <!-- Begin RelatedListElement -->
<div id="800f00000006cQt_00Na000000ASdVR" class="bRelatedList"> … </div>
  <!-- End RelatedListElement -->

  <!-- Begin RelatedListElement -->
<div id="800f00000006cQt_RelatedActivityList" class="bRelatedList"> … </div>
  <!-- End RelatedListElement -->

  <!-- Begin RelatedListElement -->
<div id="800f00000006cQt_RelatedHistoryList" class="bRelatedList"> … </div>
  <!-- End RelatedListElement -->

  <!-- Begin RelatedListElement -->
<div id="800f00000006cQt_00Na000000AT3VB" class="bRelatedList"> … </div>
  <!-- End RelatedListElement -->

  <!-- Begin RelatedListElement -->
<div id="800f00000006cQt_RelatedNoteList" class="bRelatedList"> … </div>
  <!-- End RelatedListElement -->

  <!-- Begin RelatedListElement -->
<div id="800f00000006cQt_00N3000000A6gu9" class="bRelatedList"> … </div>
  <!-- End RelatedListElement -->

  <!-- Begin RelatedListElement -->
<div id="800f00000006cQt_RelatedEntityHistoryList" class="bRelatedList"> … </div>
  <!-- End RelatedListElement -->
<div class="fewerMore"> … </div><script> … </script>
  <!-- Body events -->
<script type="text/javascript"> … </script><div id="datePicker" class="datePicker"> … </div>
  <!-- End page content -->
</td></tr></tbody></table></div><div class="bPageFooter noTableFooter"> … </div><div></div></div><script> … </script><script src="/jslibrary/1402506602000/sfdc/ChatterDefer.js" defer="true"></script><script src="/jslibrary/1404472812000/sfdc/Zen.js" defer="true"></script><script> … </script><script async="" defer="" src="/jslibrary/1400606638000/sfdc/SfdcSessionBase190.js"></script><iframe title="sessionserver" style="position: absolute; top: -999px; left: -999px;" src="https://test.salesforce.com/login/sessionserver190.html"></iframe><div id="devSlide" style="display: block;"> … </div><div id="InlineEditDialog" class="overlayDialog cssDialog inlineEditDialog" role="dialog" aria-live="assertive" aria-describedby="InlineEditDialogTitle" style="margin-top: 0px; margin-left: 0px;"> … </div>

当我检查第一个链接中的元素时,它显示带有class = datacell的表。但是页面中还有2-3个表具有相同的类名。

1 个答案:

答案 0 :(得分:1)

您可以使用links方法收集链接,然后遍历链接集合。

这是一个人为的表格作为例子:

<table class="foo"><tr>
<td><a href="http://www.example.org">one</a></td>
<td><a href="http://www.example.org">two</a></td>
</tr></table>

<table class="foo"><tr>
<td><a href="http://www.example.org">three</a></td>
<td><a href="http://www.example.org">four</a></td>
</tr></table>

<table class="bar"><tr>
<td><a href="http://www.example.org">five</a></td>
<td><a href="http://www.example.org">six</a></td>
</tr></table>

您可以收集页面上的所有链接,或根据类属性或索引位置定位特定表。

# print the text attribute for all links
b.links.each { |link| puts link.text}
#=> one
#=> two
#=> three
#=> four
#=> five
#=> six

# print the text attribute for links in the table with a class attribute of "foo"
b.table(:class => "foo").links.each { |link| puts link.text}
#=> one
#=> two

# print the text attribute for links in the table with a index position of 1
b.table(:index => 1).links.each { |link| puts link.text}
#=> three
#=> four

# print the text attribute for links in the table with a class attribute of "bar"
b.table(:class => "bar").links.each { |link| puts link.text}
#=> five
#=> six

理论上 - 一旦你收集了适当的链接集 - 你可以遍历链接和click它们(尽管我发现这容易出现StaleElementReferenceError错误。)