使用 XPath 和 Selenium for Java 获取下一个同级元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30407106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:34:44  来源:igfitidea点击:

Getting next sibling element using XPath and Selenium for Java

javaxmlseleniumxpath

提问by Vincent

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

我知道在将 Selenium 用于 Java 和 XPath 时,您可以获得一个父元素,如下所示:

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?

但是如何获取元素的下一个兄弟元素,我需要在括号之间放置什么而不是上面的情况中的两个点?

采纳答案by har07

Use following-siblingaxis :

使用following-sibling轴:

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

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

MDN 的可用轴列表,以供进一步参考:Mozilla Developer Network : Axes

回答by Vilas Kolhe

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

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

X will be the Xthsibling of that element.

X 将是该元素的第X兄弟。