I need to find an element that is located next to another one.
For example, I'm trying to retrieve the bottom with the word “Log In & Pay” only if I found the words ‘DANA’ before.
I can find the first element with text DANA in this way, but how can I find then the next botton element with the text “Log In & Pay” ?
driver.findElement(By.xpath ("//*[contains(text(), 'DANA')]"));
CodePudding user response:
Selenium 4 introduces relative locators which allow to look up elements in relative position to others. Like:
- above
- below
- right of
- left of
- and even "near"
You can find examples here.
CodePudding user response:
Get the span with the desired text, find the closest ancestor div which contains both els, find the el you want from there. i.e.
//span[contains(text(), 'DANA')]
//ancestor::div[@class='web-pay-wallet-inside-wrap']
//div[@class='action']
/div[contains(text()='Log In & Pay')]