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

Vin*_*ent 15 java xml selenium xpath

我知道你在使用Selenium for Java和XPath时可以得到一个父元素:

WebElement parent = child.findElement(By.xpath(".."));
Run Code Online (Sandbox Code Playgroud)

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

har*_*r07 33

使用following-sibling轴:

WebElement followingSibling = child.findElement(By.xpath("following-sibling::*"));
Run Code Online (Sandbox Code Playgroud)

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

  • 要获得下一个兄弟,请使用`following-sibling ::*[1]`. (9认同)

小智 7

WebElement父= child.findElement(By.xpath(“ following-sibling :: * [X]”));

X将是该元素的第X 同级。