welcome
username
Now the problem is that I...">

Pre navigation in selenium webdriver

I have the following HTML

<div id="element1">welcome</div> <div>username</div> 

Now the problem is that I have to select the next div element next to "element1". I know I can build an xpath from my parent. But I need to choose the one that is next to the item. How is this possible?

+4
source share
2 answers

Use the following-sibling axis .

This selects all sibling <div> elements after element1 node:

 id('element1')/following-sibling::div 

This selects the first sibling <div> element after element1 node:

 id('element1')/following-sibling::div[1] 

This selects the first sibling node after element1 node if it is a <div> :

 id('element1')/following-sibling::*[1][local-name(.)='div'] 

All these three examples will be chosen by your element (and perhaps a little more if there are more matches), so choose the one that suits your intentions best.


Note that you can do all this with a simple CSS selector . This is equivalent to the latest XPath. And it will be faster.

 #element1 + div 
+6
source

You can use the following-sibling :

 driver.findElement(By.id("element1")).findElement(By.xpath("./following-sibling::div")); 
+3
source

Source: https://habr.com/ru/post/1499102/


All Articles