So my question is, is there an xpath that will return the one element that immediately precedes a given element?Īs per the HTML you have shared to locate the with id="value2" with respect to with Value 2 you can use the following xpath. Selenium sets the value of input to be the input with id='value1', the first in the returned array. So, in the example, I get an array of two inputs returned, both 'value1' and 'value2'. I thought I could use this: WebElement input = element.findElement(By.xpath("preceding-sibling::input")) īut that xpath actually returns all the inputs that are siblings of the label. Since it has to be relative to the label I've found, I can't just find by that id I don't really know the id, just that the input is supposed to precede the label. Lets say I have found the 'Value 2' label as a WebElement, then I want to do next is find the specific input immediately preceding that label (id='value2'). The problem is, using preceding-sibling from xpath, I get all elements preceding the found element, not the one immediately preceding it. The code needs to be dynamic because I don't know if the element I start from is the 3rd out of 5, the 6th out of 10, or the 1st out of 2. I am running tests in Java Selenium where I find an element, and once found need to find the immediately preceding element.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |