MrBridger MrBridger - 4 months ago 21
Java Question

Selenium WebDriver Java cssSelector Span

I am trying to click on a link using Selenium WebDriver in Java. My Java:

driver.findElement(By.cssSelector("span[data-seleniumid=\"Address0\"]")).click();


The HTML on my page looks like this:

<span data-seleniumid="Address0" class="ATAddressLine">1 The Road, Town, City, Postcode</span>


The error in Eclipse is:

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"span[data-seleniumid=\"Address0\"]"}


Thanks

Answer

Thanks for you help all. The element wasn't being found because it was in an iframe popup and Selenium was searching for it in the page behind.

This post: http://stackoverflow.com/a/32836709/6565982 helped.

For anyone in the future my code is now:

WebElement iFrame= driver.findElement(By.tagName("iframe"));
        driver.switchTo().frame(iFrame);

        // Select an address
        driver.findElement(By.cssSelector("span[data-seleniumid=\"Address0\"]")).click();

        // Switch back to the default page
        driver.switchTo().defaultContent();

Thanks again.

Comments