我正在尝试从html表td中检索所有元素,但是当我尝试通过具有多个条件的xpath查找元素时,它无法检索0个元素,因此在单个条件下获取所有值没有问题。到目前为止,我的问题可能是在html元素的顺序中。
这是使用xpath按条件进行搜索的方法,效果很好:
"//input[@class='managebtn'][@value='repost']"
这是我要达到的目标而没有达到的结果:
"//td[contains(text(),'manage')]/div[@class='regular']/form[@class='manage']/input[@class='managebtn'][@value='repost']"
以下html表是我要阅读的表”
<tbody aria-live="polite" aria-relevant="all">
<tr class="posting-row odd ui-state-default" role="row">
<td class="status expired">
<small class="gc" style="background:">
Expired </small>
</td>
<td class="buttons expired"></td>
</tr>
<tr class="posting-row even ui-widget-content" role="row">
<td class="status deleted">
<small class="gc" style="background:">
Deleted </small>
</td>
<td class="buttons deleted">
<div class="regular">
<form action="https://" method="GET" class="manage display">
<input type="hidden" name="action" value="display">
<input type="submit" name="go" value="display" class="managebtn">
</form>
<form action="https://" method="GET" class="manage ">
<input type="hidden" name="action" value="repost">
<input type="submit" name="go" value="repost" class="managebtn">
</form>
</div>
</td>
</tr>
<tr class="posting-row odd ui-state-default" role="row">
<td class="status deleted">
<small class="gc" style="background:">
Deleted </small>
</td>
<td class="buttons deleted">
<div class="regular">
<form action="https://" method="GET" class="manage display">
<input type="hidden" name="action" value="display">
<input type="submit" name="go" value="display" class="managebtn">
</form>
<form action="https://" method="GET" class="manage ">
<input type="hidden" name="action" value="repost">
<input type="submit" name="go" value="repost" class="managebtn">
</form>
</div>
</td>
</tr>
<tr class="posting-row even ui-widget-content" role="row">
<td class="status deleted">
<small class="gc" style="background:">
Deleted </small>
</td>
<td class="buttons deleted">
<div class="regular">
<form action="https://" method="GET" class="manage display">
<input type="hidden" name="action" value="display">
<input type="submit" name="go" value="display" class="managebtn">
</form>
<form action="https://" method="GET" class="manage ">
<input type="hidden" name="action" value="repost">
<input type="submit" name="go" value="repost" class="managebtn">
</form>
</div>
</td>
</tr>
仅当第一列的值是 Expired (过期)时,我才想从第二列(值='repost')中检索所有值。
由于该表在列中包含多个元素,因此可能看起来有些疏忽。
由于这些列的内容如下:
<th data-column="0" class="tablesorter-header tablesorter-headerUnSorted ui-widget-header ui-corner-all ui-state-default" tabindex="0" scope="col" role="columnheader" aria-disabled="false" unselectable="on" style="user-select: none;" aria-sort="none" aria-label="status: No sort applied, activate to apply an ascending sort">
<div class="tablesorter-wrapper" style="position:relative;height:100%;width:100%">
<div class="tablesorter-header-inner">
status <i class="tablesorter-icon ui-icon ui-icon-carat-2-n-s"></i>
</div>
</div>
</th>
<th class="sorter-false tablesorter-header tablesorter-headerUnSorted ui-widget-header ui-corner-all ui-state-default" data-column="1" scope="col" role="columnheader" aria-disabled="true" unselectable="on" style="user-select: none;" aria-sort="none" aria-label="manage: No sort applied, sorting is disabled">
<div class="tablesorter-wrapper" style="position:relative;height:100%;width:100%">
<div class="tablesorter-header-inner">
manage <i class="tablesorter-icon ui-icon"></i>
</div>
</div>
</th>
我不知道是否有可能这样的一行代码来获取我需要的值:
driver.findElement(By.xpath("//th[@class='sorter-false tablesorter-header tablesorter-headerUnSorted ui-widget-header ui-corner-all ui-state-default' and @text()='manage']"));
这是表格的样子:
答案 0 :(得分:-1)
要在状态 为已过期的项目中将属性为 value 的元素标识为repost
,您必须得出{{ 3}}作为VisibilityOfAllElementsLocatedBy()
,则可以使用以下基于WebDriverWait的xpath:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.XPath("//tr[contains(@class, 'posting-row')][./td[@class='status expired']]//td[@class='buttons expired']//input[@class='managebtn' and @value='repost']")));
通过 SeleniumExtras.WaitHelpers ,您可以使用:
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.XPath("//tr[contains(@class, 'posting-row')][./td[@class='status expired']]//td[@class='buttons expired']//input[@class='managebtn' and @value='repost']")));