Java 查找相对于其他WebElement的WebElement
我试图找到一个相对于另一个Web元素的Web元素,而不是使用Selenium的另一个Web元素的子元素。这是我正在使用的源代码的一个片段Java 查找相对于其他WebElement的WebElement,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我试图找到一个相对于另一个Web元素的Web元素,而不是使用Selenium的另一个Web元素的子元素。这是我正在使用的源代码的一个片段 <td class="action" rowspan="7"> <table class="action"> <tbody> <tr class="topTr topTrPlainBg"> <tr>
<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>
知道我做错了什么吗?我尝试过使用斜杠,但没有任何效果。查看您使用的
xpath
,我看到的是您缺少首字母/
,因此请替换您使用的xpath
//..//..//..//..//../td//img[@title='Add item']
查看您正在使用的
xpath
我看到的是您缺少初始/
,因此请将您正在使用的xpath
替换为
//..//..//..//..//../td//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();
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();
这里我们得到了a
元素,其中Foo-Bar
文本正在检查父类。然后,对于第一个td
父级,使用rule
类获得以下td
同级。然后,获取所需的img
元素
如果要从找到的a
元素开始,请使用class=“action”
查找td
父元素,然后应用以下同级检查:
ancestor::td[@class="action"]/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();
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();
如果您稍微改变一下策略并使用: 在硒中:
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();
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();
这里我们得到了a
元素,其中Foo-Bar
文本正在检查父类。然后,对于第一个td
父级,使用rule
类获得以下td
同级。然后,获取所需的img
元素
如果要从找到的a
元素开始,请使用class=“action”
查找td
父元素,然后应用以下同级检查:
ancestor::td[@class="action"]/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();
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。@如果它们是两个不同的表。我仍然想在紧跟在另一个表后面的表中找到一个元素。@user215050那么,
Foo bar
的位置总是在第二个td中?还是这样改变了?@Saifur我也是,但他要求提供一个相对于WebElement的Xpath,这就是它的工作原理。对我来说似乎是两个不同的表?!检查我的答案,你必须以点开始Xpath。@如果它们是两个不同的表。我仍然想在紧跟在另一个表后面的表中找到一个元素。@user215050那么,Foo bar
的位置总是在第二个td中?还是这样改变了?@Saifur我也是,但他要求提供一个相对于WebElement的Xpath,这就是它的工作原理。嘿,对不起,我的文章的代码部分有一个拼写错误,显示了我迄今为止所做的尝试。现在您可以看到我是如何试图找到相对于其他元素的元素的。初始的/
不是必需的,因为我正在查找相对于其他元素的此webelement,而不是相对于源代码的此webelement。有了开头的/
,它基本上就是在整个html源代码中单击第一个添加项。@user215050我不确定是不是这样?你试过/
吗?我试过了。。。它在整个html源代码中单击第一个additem
,而不是紧跟在td
之后的Foo-bar
childOK。我知道您已经提到,您需要Foo bar
的相对元素。考虑到您知道的Foo bar
的位置始终是2,我们可以使用此cssSelector.rule.rulePlainBg td:nth child(2)>img
以及Hey抱歉,我的帖子的代码部分有一个打字错误,显示了我迄今为止的尝试。现在您可以看到我是如何试图找到相对于其他元素的元素的。初始的/
不是必需的,因为我正在查找相对于其他元素的此webelement,而不是相对于源代码的此webelement。有了开头的/
,它基本上就是在整个html源代码中单击第一个添加项。@user215050我不确定是不是这样?你试过/
吗?我试过了。。。它在整个html源代码中单击第一个additem
,而不是紧跟在td
之后的Foo-bar
childOK。我知道您已经提到,您需要Foo bar
的相对元素。考虑到您知道Foo bar
的位置始终为2,我们可以使用此cssSelector.rule.rulePlainBg td:nth child(2)>img
以及您是否介意在我的问题和Selenium以及所有内容的上下文中完整地拼写您的两个答案?比如拥有driver.findElement(By.xpath())
等等?非常感谢。@user215050当然,已更新。作为旁注,确保Add Item确实是可点击的,并且smth将在点击img
元素后发生。出色且可靠的答案。非常感谢您。您介意在我的问题和Selenium以及所有内容的上下文中完整地拼写出您的两个答案吗?比如拥有driver.findElement(By.xpath())
等等?非常感谢。@user215050当然,已更新。作为旁注,确保Add Item确实是可点击的,并且smth将在点击img
元素后发生。出色且可靠的答案。非常感谢你。