I'm parsing through some HTML/Javascript pages in Watir, and I've run into a little trouble differentiating between two links on a sub-table within a dropdown menu. There are many instances of this within my tables (which I can't change) like 'Overview' and 'Events'.
我正在解析Watir中的一些HTML / Javascript页面,并且我在下拉菜单中的子表上区分两个链接时遇到了一些麻烦。我的表格中有很多这样的例子(我无法改变),例如“概述”和“事件”。
<td class="nav-td-content-submenu wide-text smaller-text" valign="top">
<a href="javascript: menuManager.check('/Manager/navigate?menuID=system_overview');"
class="report-group-toggle"
style="color:black"
id="report_group_servers"> Servers
</a>
<a href="javascript: menuManager.check('/Manager/navigate?menuID==system_overview');"
title="Overview"
class="report-group-link">
<div>Overview</div>
</a>
<a href="javascript: menuManager.check('/Manager/SavedReportExec.do?value(reportID)=61&value(menuID)=system.1');"
title="Events"
class="report-group-link">
<div>Events</div>
</a>
<a href="javascript: menuManager.check('/Manager/navigate?menuID=alerts');"
title="Alerts"
class="report-group-link">
<div>Alerts</div>
</a>
<a href="javascript: menuManager.check('/Manager/navigate?menuID=logs');"
title="Logs"
class="report-group-link">
<div>Logs</div>
</a>
<a href="javascript: menuManager.check('/Manager/SavedReportExec.do?value(reportID)=62&value(menuID)=agent.1');"
class="report-group-toggle"
style="color:black"
id="report_group_agents"> Agents
</a>
<a href="javascript: menuManager.check('/Manager/SavedReportExec.do?value(reportID)=62&value(menuID)=agent.1');"
title="Overview"
class="report-group-link">
<div>Overview</div>
</a>
<a href="javascript: menuManager.check('/Manager/SavedReportExec.do?value(reportID)=63&value(menuID)=agent.2');"
title="Events"
class="report-group-link">
<div>Events</div>
</a>
<a href="javascript: menuManager.check('/Manager/navigate?menuID=application_monitoring');"
title="Application Monitoring"
class="report-group-link">
<div>Application Monitoring</div>
</a>
Where the dropdown menu looks like so:
下拉菜单如下所示:
Servers
- Overview
- Events
- Alerts
- Logs
Agents
- Overview
- Events
- Application Monitoring
The link for System directly navigates to the first "Overview" within the subsection. (Note: The main menu icon is also the link for the Servers->Overviews page).
系统的链接直接导航到子节中的第一个“概述”。 (注意:主菜单图标也是Servers-> Overviews页面的链接)。
My problem is that I'm trying to differentiate between Overview as it pertains to Servers->Overview and Agents->Overview.
我的问题是我试图区分概述,因为它与服务器 - >概述和代理 - >概述有关。
I can check for the existence and navigate to Servers->Overview like so:
我可以检查存在并导航到Servers-> Overview,如下所示:
if $browser.link(:id => 'report_group_servers').exists?
if $browser.link(:id => 'report_group_servers').exists?
if $browser.link(:title => 'Overview').exists?
$browser.link(:title => 'Overview').click
end
end
end
However even when I try to get to Agents->Overview I am still navigating to Servers->Overview so I try:
然而,即使我尝试访问代理 - >概述,我仍然导航到Servers-> Overview,所以我尝试:
if $browser.link(:id => 'report_group_agents').exists?
if $browser.link(:id => 'report_group_agents').exists?
if $browser.link(:title => 'Overview', :id => 'report_group_agents').exists?
$browser.link(:title => 'Overview', :id => 'report_group_agents').click
end
end
end
In the attempt to just look in the main link of the table (since the Servers links to Servers->Overview and Agents links to Agents->Overview) I also tried to select the link that contains "Agents":
在尝试查看表的主链接时(因为服务器链接到Servers-> Overview and Agents链接到Agents-> Overview),我还尝试选择包含“Agents”的链接:
if $browser.link(:xpath, "//a[contains(.,'Agents')]/")
$browser.link(:xpath, "//a[contains(.,'Agents')]/").click
end
This however generates an error since the 'contains' is an evaluation, and this checks the entire page for any links that contain Agents - which could be problematic.
然而,这会产生错误,因为'contains'是一个评估,它会检查整个页面是否包含代理的任何链接 - 这可能会有问题。
Considering all links of the sub-table are of the class "report-group-link", how might I be able to differentiate between the two links based off of the first id or reference name?
考虑到子表的所有链接都是“report-group-link”类,我怎样才能根据第一个id或引用名来区分这两个链接?
1 个解决方案
#1
1
Solution 1 - Use text and href attribute
解决方案1 - 使用text和href属性
The 2 Overview links are differentiable by their text (from other links) and by their href attribute (from each other). I think the cleanest solution would be to use these two attributes:
2概述链接可通过其文本(来自其他链接)和其href属性(来自彼此)区分。我认为最干净的解决方案是使用这两个属性:
# The Servers > Overview link
browser.link(:text => 'Overview', :href => /system_overview/)
# The Agents > Overview link
browser.link(:text => 'Overview', :href => /SavedReportExec/)
Solution 2 - Xpath
解决方案2 - Xpath
If you really need/want to find the link based on its relationship to the Servers and Agents links, you could use the preceding-sibling axis in xpath.
如果您确实需要/想要根据其与“服务器和代理”链接的关系找到链接,则可以在xpath中使用前一个兄弟轴。
# The Servers > Overview link
browser.link(:xpath => '//a[preceding-sibling::a[contains(., "Servers")]][contains(., "Overview")]')
# The Agents > Overview link
browser.link(:xpath => '//a[preceding-sibling::a[contains(., "Agents")]][contains(., "Overview")]')
Solution 3 - CSS
解决方案3 - CSS
The xpath is pretty hard to read, so you could use a css-selector (with the general sibling selector ~
) instead.
xpath非常难以阅读,因此您可以使用css-selector(使用通用兄弟选择器〜)代替。
# The Servers > Overview link
browser.link(:css => '#report_group_servers ~ a[title="Overview"]')
# The Agents > Overview link
browser.link(:css => '#report_group_agents ~ a[title="Overview"]')
#1
1
Solution 1 - Use text and href attribute
解决方案1 - 使用text和href属性
The 2 Overview links are differentiable by their text (from other links) and by their href attribute (from each other). I think the cleanest solution would be to use these two attributes:
2概述链接可通过其文本(来自其他链接)和其href属性(来自彼此)区分。我认为最干净的解决方案是使用这两个属性:
# The Servers > Overview link
browser.link(:text => 'Overview', :href => /system_overview/)
# The Agents > Overview link
browser.link(:text => 'Overview', :href => /SavedReportExec/)
Solution 2 - Xpath
解决方案2 - Xpath
If you really need/want to find the link based on its relationship to the Servers and Agents links, you could use the preceding-sibling axis in xpath.
如果您确实需要/想要根据其与“服务器和代理”链接的关系找到链接,则可以在xpath中使用前一个兄弟轴。
# The Servers > Overview link
browser.link(:xpath => '//a[preceding-sibling::a[contains(., "Servers")]][contains(., "Overview")]')
# The Agents > Overview link
browser.link(:xpath => '//a[preceding-sibling::a[contains(., "Agents")]][contains(., "Overview")]')
Solution 3 - CSS
解决方案3 - CSS
The xpath is pretty hard to read, so you could use a css-selector (with the general sibling selector ~
) instead.
xpath非常难以阅读,因此您可以使用css-selector(使用通用兄弟选择器〜)代替。
# The Servers > Overview link
browser.link(:css => '#report_group_servers ~ a[title="Overview"]')
# The Agents > Overview link
browser.link(:css => '#report_group_agents ~ a[title="Overview"]')