使用XPath和Java的Selenium获取下一个兄弟元素

时间:2022-11-28 00:26:02

I know that you can get a parent element when using Selenium for Java and XPath like this:

我知道在Java和XPath中使用Selenium时,可以得到父元素:

WebElement parent = child.findElement(By.xpath(".."));

But how do I get the next sibling of an element, what do I need to put between the parentheses instead of two dots as in the case above?

但是如何得到一个元素的下一个兄弟元素,我需要在圆括号之间放什么,而不是像上面那样放两个点?

2 个解决方案

#1


25  

Use following-sibling axis :

使用祖辈轴:

WebElement followingSibling = child.findElement(By.xpath("following-sibling::*"));

List of available axes by MDN, for further reference : Mozilla Developer Network : Axes

MDN提供的可用轴列表,以供进一步参考:Mozilla开发人员网络:axes

#2


4  

WebElement parent = child.findElement(By.xpath("following-sibling::*[X]"));

WebElement父母= child.findElement(By.xpath(“祖辈::*[X]”));

X will be the Nth sibling of that element.

X是这个元素的第n个兄弟元素。

#1


25  

Use following-sibling axis :

使用祖辈轴:

WebElement followingSibling = child.findElement(By.xpath("following-sibling::*"));

List of available axes by MDN, for further reference : Mozilla Developer Network : Axes

MDN提供的可用轴列表,以供进一步参考:Mozilla开发人员网络:axes

#2


4  

WebElement parent = child.findElement(By.xpath("following-sibling::*[X]"));

WebElement父母= child.findElement(By.xpath(“祖辈::*[X]”));

X will be the Nth sibling of that element.

X是这个元素的第n个兄弟元素。