MrBridger
MrBridger

Reputation: 11

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

Upvotes: 0

Views: 3221

Answers (4)

MrBridger
MrBridger

Reputation: 11

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: https://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.

Upvotes: 0

neonidian
neonidian

Reputation: 1263

Have a webdriver wait condition like waiting for an element to be clickable and then use your above code.

Upvotes: 0

Buster
Buster

Reputation: 715

I would try a different selector like "span.ATAddressLine". Not sure if webdriver likes your attribute "data-seleniumid".

Upvotes: 0

jaredgilmore
jaredgilmore

Reputation: 657

Instead of trying to escape the inner double quotes, just use a single quote instead.

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

Upvotes: 1

Related Questions