[英]Finding WebElement Relative to Another WebElement
我试图找到一个相对于另一个 webelement 的 webelement,该 webelement 不是另一个使用 Selenium 的子元素。 这是我正在使用的源代码片段...
<td class="action" rowspan="7">
<table class="action">
<tbody>
<tr class="topTr topTrPlainBg">
<tr>
<td class="middleLeftPlainBg" style="width: 4px;">
<td class="caption" style="width: 99%; height: 215px;" colspan="4" title="Action properties">
<a href="javascript: var none=0;">Foo bar</a>
</td>
<td class="middleRightPlainBg" style="width: 4px;">
</tr>
<tr class="bottomTr bottomTrPlainBg">
</tbody>
</table>
</td>
<td class="rule rulePlainBg" colspan="1">
<table class="rule rulePlainBg">
<tbody>
<tr>
<td class="captionRule">Successful</td>
<td class="plus">
<img src="https://mcc-69-77.usae.bah.com/sam/admin/vpe2/public/img/vpe-old/rule/plus-over.gif" title="Add item"/>
</td>
<td class="swap">
</tr>
</tbody>
</table>
</td>
我正在尝试相对于值为“Foo bar”的a
元素查找标题为“添加项目”的img
元素。 我知道我正在寻找的元素将是紧跟在相关元素父元素td
之后的td
的子元素。 这是我到目前为止所拥有的(在 Java 中)......
WebElement fooBar = driver.findElement(By.linkText("Foo bar"));
fooBar.findElement(By.xpath("..//..//..//..//../td//img[@title='Add item']")).click();
任何想法我在这里做错了什么? 我试过稍微玩弄斜线,但什么也做不了。
如果您稍微改变策略并使用following-sibling
怎么办:
//td[@class="action" and .//td[@class="caption"]/a[. = "Foo Bar"]]/following-sibling::td[contains(@class, "rule")]//td[@class="plus"]/img[@title="Add item"]
在硒中:
WebElement addItem = driver.findElement(By.xpath("//td[@class='action' and .//td[@class='caption']/a[. = 'Foo Bar']]/following-sibling::td[contains(@class, 'rule')]//td[@class='plus']/img[@title='Add item']"));
addItem.click();
在这里,我们正在获取带有Foo Bar
文本的a
元素,在途中检查父类的类。 然后,对于第一个td
父级,获得以下具有rule
类的td
兄弟级。 然后,获取所需的img
元素。
如果你想从找到的a
元素开始,使用ancestor
找到class="action"
的td
父元素,然后应用以下兄弟检查:
ancestor::td[@class="action"]/following-sibling::td[contains(@class, "rule")]//td[@class="plus"]/img[@title="Add item"]
在硒中:
WebElement fooBar = driver.findElement(By.linkText("Foo bar"));
fooBar.findElement(By.xpath("ancestor::td[@class='action']/following-sibling::td[contains(@class, 'rule')]//td[@class='plus']/img[@title='Add item']")).click();
查看您正在使用的xpath
,我看到的是您缺少初始值//
因此请替换您正在使用的xpath
//..//..//..//..//../td//img[@title='Add item']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.