Selenium Webdriver Nested Elements (JAVA)

How to access a nested element without using xpath

this is how i write it in Selenium WebDriver (Ruby)

@browser.find_element(:class, 'mapLock').find_element(:class => 'mapLockOverlay').click 

But as I write it in JAVA I tried:

 browser.findElement(By.className("mapLock").findElement(By.className("mapLockDisplay").click 

which, as I know, is clearly wrong

+4
source share
1 answer

You are actually pretty close, just keep the brackets. I just separated things a bit.

 final WebElement mapLockElement = browser.findElement(By.className("mapLock")); final WebElement mapLockDisplayElement = mapLockElement.findElement("mapLockDisplay"); mapLockDisplayElement.click(); 

If you do it all on one line, it will be

 browser.findElement(By.className("mapLock")).findElement(By.className("mapLockDisplay")).click(); 
+7
source

All Articles