0

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

1
  • What else have you tried other than just the one line of code? I would recommend you look at some CSS references and tutorials so that you can learn other ways to create selectors so you can try varying locator methods. Commented Jul 8, 2016 at 18:22

4 Answers 4

1

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

driver.findElement(By.cssSelector("span[data-seleniumid='Address0']")).click();
Sign up to request clarification or add additional context in comments.

1 Comment

Your original answer was too close to a code only answer which is not allowed. In the future, please just add a few comments on what your code does differently to solve the problem so that the OP and future readers can easily see and understand the difference.
0

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

1 Comment

Selenium doesn't like or dislike attributes. If done correctly, any attribute will work.
0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.