I'm looking for a generic way to get the next element within Selenium. Jquery has a .next() function and overloads to allow a given type so I imagine there may be a JS based solution available for Selenium.
Given it's such a useful feature I'd have imagined someone must have done it already by now.
Cheers, Jamie
WebElement parent = child. findElement(By. xpath(".."));
To get the immediate next sibling element of given web element using Selenium for Java, find element on the given web element by XPath with the XPath expression as "following-sibling::*" .
To get all the sibling elements of a given web element using Selenium for Java, Find the following elements on the given web element by XPath with the XPath expression as "following-sibling::*" . Find the preceding elements on the given web element by XPath with the XPath expression as "preceding-sibling::*" .
Both xOffset and yOffset represent the relative pixel distance (integer) with which to scroll. For xOffset , positive value means to scroll right, and negative value means to scroll left. For yOffset , positive value means to scroll downward, and negative value means to scroll upward.
I'm going to assume you mean when you select an element some selector (say By.className("primary")
), and you want to get the next item that has that class name?
To do that, you need to call driver.findElements(By.className("primary"))
. This will give you a list of all of the elements that match that selector. Then, you can pick whichever one suits your needs.
If however, next() actually returns the next sibling, then you can do it these ways:
(Replace element with your selector)
One of the possible ways to navigate to element under 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:
android.widget.TextView[@text = "Current element text"]
android.widget.RelativeLayout
and select the next android.widget.TextView[@text="Next element text"]
under the same hierarchy.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With