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

MrBridger
  • 11
  • 1
  • 3
  • 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. – JeffC Jul 08 '16 at 18:22

4 Answers4

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();
JeffC
  • 22,180
  • 5
  • 32
  • 55
jaredgilmore
  • 657
  • 8
  • 17
  • 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. – JeffC Jul 08 '16 at 18:19
0

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

JeffC
  • 22,180
  • 5
  • 32
  • 55
Buster
  • 685
  • 1
  • 5
  • 28
  • Selenium doesn't like or dislike attributes. If done correctly, any attribute will work. – JeffC Jul 08 '16 at 18:20
0

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

neonidian
  • 1,221
  • 13
  • 20
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.

Community
  • 1
  • 1
MrBridger
  • 11
  • 1
  • 3