Is it possible to get the next item in Selenium

I am looking for a general way to get the next item in Selenium. JQuery has a .next () function and overloads to allow this type, so I assume that a JS-based solution may be available for selenium.

Given that this is such a useful feature, I would suggest that someone has already done this by now.

Cheers, Jamie

+6
source share
2 answers

I'm going to assume that you mean when you select an element, some kind of selector (say By.className("primary") ), and you want to get the next element that has this class name?

To do this, you need to call driver.findElements(By.className("primary")) . This will give you a list of all the elements matching this selector. Then you can choose the one that suits you.

If, however, next () actually returns the next sibling, then you can do this in the following ways:

  • Javascript: element.nextSibling ()
  • CSS: element + *
  • Xpath: element / follow-sibling :: *

(Replace the item with your selector)

+3
source

One possible way to jump to an element under the same hierarchy is to use /../ in xpath, as shown below:

 current_element = driver.find_element_by_xpath('//android.widget.RelativeLayout/android.widget.TextView[@text="Current element text"]/../android.widget.TextView[@text="Next element text"]') 

Here it will be:

  • First go to android.widget.TextView[@text = "Current element text"]
  • It will then return to the parent element ie android.widget.RelativeLayout and select the next android.widget.TextView[@text="Next element text"] under the same hierarchy.
0
source

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


All Articles