如何在java selenium中单击动态<li>元素?

时间:2017-01-31 09:31:10

标签: java selenium selenium-webdriver

我想点击第4个列表元素,它在html代码下面展开。

xpath每次都与//*[@id='filter_result_totals']/ul/li[4]/a

相同

元素值 1946 总是在变化,它表示为记录数。

<document>
<html>
<head>
<body style="overflow-y: hidden;">
<div class="container">
<div id="header" class="header">
<div class="main-menu menu menu-left">
<div class="headercontent">
<div style="display: none">
<div class="finder">
<div id="profile" class="profile menu">
<div class="clear"/>
<div class="main-toolbar">
<div class="main-toolbar-title">List view</div>
<div class="main-toolbar-subtitle">
<div class="main-toolbar-info">
<div id="filter_result_totals">
<ul>
<li class="extra_toolbar_category">
<li>
<li>
<li>
<a class="query_link" href="/monitor/index.php/listview?q=%5Bservices%5D%20state!%3D0%20and%20acknowledged%3D0%20and%20scheduled_downtime_depth%3D0%20and%20host.scheduled_downtime_depth%3D0%20and%20state%3D2%20and%20has_been_checked%3D1" data-query="[services] state!=0 and acknowledged=0 and scheduled_downtime_depth=0 and host.scheduled_downtime_depth=0 and state=2 and has_been_checked=1" title="Services critical">
<span class="icon-16 x16-shield-critical" title="Services critical"/>
1946
</a>
</li>
<li>
<li>
</ul>
</div>
</div>
<div class="main-toolbar-buttons toolbar-buttons">
<div class="clear"/>
</div>
</div>
<div id="page_settings" class="page_settings">
<div id="content" class="content " tabindex="0" style="height: 605px;">
<div class="jq-notify-zone"/>
<script type="text/javascript">$.notify.sessionid = '9723b1738ffd58a3a48627f3f4a39e6bad230cd2';$.notify.configured = {};</script>
</div>
<ul id="property_menu" class="contextMenu">
<ul id="svc_property_menu" class="contextMenu">
<div id="fancybox-tmp"/>
<div id="fancybox-loading">
<div id="fancybox-overlay"/>
<div id="fancybox-wrap">
<div id="cboxOverlay" style="display: none;"/>
<div id="colorbox" class="" style="display: none;">
<div id="AutocompleteContainter_1485854680035" style="position: absolute; z-index: 99999; top: 36.7667px; left: 1353.13px;">
</body>
</html>
</document>

任何人都可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

这可能是时间问题。尝试设置隐式等待

a2.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

或使用显式等待

WebDriverWait wait = new WebDriverWait(a2, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='filter_result_totals']/ul/li[4]/a")));
element.click();

请参阅waits

答案 1 :(得分:0)

您可以尝试使用以下内容:

driver.findElement(By.cssSelector("a[class='query_link'][title='Services critical']")).click();