简体   繁体   中英

Selenium Webdriver(JAVA) Nested Elements

How do I access a nested element without using xpath

this is how I would write it in Selenium WebDriver (Ruby)

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

But how would I write it in JAVA I have tried:

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

which I know is obviously wrong

You're actually pretty close, just mind 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're doing it all on one line, it would be

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM