0

So I'm working on this project and I need to find the correct locator for Selenium testing. I've tried a bunch of different combinations and I can't find the right one to click this link. Everything I try throws a No Such Element exception.

I need to click the first link in the unordered list. Here is the HTML

<main id="content" role="main">
  <nav id="product-list" role="navigation">
    <ul>
      <li id="firstTile">
        <a class="productLink" href="***LINK***" target="_top">
          <img src="***image ref***">
          <p>
            <span class="productName" title="First Tile"></span>
            <br>
            <small>This is the text for the first tile</small>
          </p>
        </a>
        <div class="item-footer cf">
          <hr>
      </li>
      <li id="secondTile">
        <li id="thirdTile">
          <li id="fourthTile" class="coming-soon">
    </ul>
  </nav>
  <div class="extra-links cf">
</main>

Here is my Java:

Test class

// All imports

public class Test {

    private Home home;
    //All other variables

    @Before
    public void setup() {
        driver = new FirefoxDriver();
    }

    @Test
    public void clickFirstTile() {
        home = new Home(driver);
        home.clickFirstTile();
    }

    @After
    public void tearDown() {
        driver.quit();
    }
}

And also my Home Page Object class:

public class Home {

    private WebDriver driver;
    By firstTileBtnLocator = By.id("firstTile");

    public Home(WebDriver driver) {
        this.driver = driver;
        // I've replaced my site with this fake url. The real site url works.
        driver.navigate().to("http://myfakesite.com");
    }

    public void clickFirstTile() {
        driver.findElement(firstTileBtnLocator).click();
    }
}
Alex
  • 15
  • 5

4 Answers4

0

Try the following:

By firstHref = By.cssSelector("li[id='firstTile'] a");

or

By firstHref = By.cssSelector("a.productLink");
Moe Ghafari
  • 2,227
  • 1
  • 12
  • 17
  • (Sorry, I'm trying to get StackOverflow to format the code below. I'm new to this.) I'm still getting the No Such Element exception. For the sake of simplicity (for the time being), I've tried bypassing the Page Object class and placing all the code inside the Test class. So here's what I have in the clickFirstTile method: ` @Test public void clickFirstTile() { home = new Home(driver); By firstHref = By.cssSelector("li[id='firstTile'] a"); driver.findElement(firstHref).click(); } ` – Alex Jul 19 '16 at 18:08
  • Its ok, can you try one more thing please? By.cssSelector("a.productLink"); – Moe Ghafari Jul 19 '16 at 18:29
0

I would suggest a different locator

By firstTileBtnLocator = By.cssSelector("#firstTile a.product-link");

I'm guessing there are a number of

<a class="productLink" href="***LINK***" target="_top">

elements on the page. The CSS Selector above will target only the one under <li id="firstTile">.


After seeing the site, the problem is that the element is inside an IFRAME and there was a mistake in the HTML you posted (incorrect id). I've tried the code below and it works.

driver.get("http://www.wolframcloud.com/");
driver.switchTo().frame(0);
driver.findElement(By.cssSelector("#wdp-tile a.product-link")).click();
driver.switchTo().defaultContent();

You would edit a couple places in your classes.

By firstTileBtnLocator = By.cssSelector("#wdp-tile a.product-link");

and

public void clickFirstTile() {
    driver.switchTo().frame(0);
    driver.findElement(firstTileBtnLocator).click();
}

I think that should take care of it.

More info on dealing with IFRAMEs.

How to switch between frames in Selenium WebDriver using Java

Community
  • 1
  • 1
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • That's what I was thinking! And you're right, there are multiple list items all with the same class. I just was unsure of how to format the arguments passed into the cssSelector method. But it's still throwing that exception! I'm so confused. Is it possible I'm trying to find the element before the link is loaded? Do I need a wait method? – Alex Jul 19 '16 at 18:50
  • So just to make things simpler... here's the example I'm following. I'm trying to click the first link at wolframcloud dot com. – Alex Jul 19 '16 at 18:53
  • I added a wait example to my answer. See if that helps. – JeffC Jul 19 '16 at 18:54
  • Ah... now that I see the site, I see what the problem is. I've updated my answer with working code. – JeffC Jul 19 '16 at 19:04
0

You should try using WebDriverWait to wait until element to be clickable as below :-

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("This is the text for the first tile")));
el.click();

Hope it helps...:)

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
0

Please try this:

wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.name("nameofframe")));

driver.findElement(By.xpath("//a[@target='_top' and @class='productLink']")).getAttribute("href");

If you have more than one a with target='_top' and class='productLink', you can find elements in list

List<WebElement> anchors=driver.findElements(By.xpath("//a[@target='_top' and @class='productLink']"));

then iterate over anchors list and click the 0th element like

anchors.get(0).click();

Let me know if it is working

Pratty
  • 85
  • 2
  • 13