Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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

share|improve this question

1 Answer 1

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();
share|improve this answer
7  
Or even better, browser.findElement(By.css(".mapLock .mapLock display")).click(). –  Ross Patterson Feb 5 '13 at 22:41
    
Indeed! Wasn't something that came to my mind, as I'm not a web developer most days, but a great solution nonetheless :) –  Justin Jasmann Feb 5 '13 at 23:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.