Alex Alex - 1 year ago 66
HTML Question

Looking for correct Locator for Selenium testing

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">
<li id="firstTile">
<a class="productLink" href="***LINK***" target="_top">
<img src="***image ref***">
<span class="productName" title="First Tile"></span>
<small>This is the text for the first tile</small>
<div class="item-footer cf">
<li id="secondTile">
<li id="thirdTile">
<li id="fourthTile" class="coming-soon">
<div class="extra-links cf">

Here is my Java:

Test class

// All imports

public class Test {

private Home home;
//All other variables

public void setup() {
driver = new FirefoxDriver();

public void clickFirstTile() {
home = new Home(driver);

public void tearDown() {

And also my Home Page Object class:

public class Home {

private WebDriver driver;
By firstTileBtnLocator ="firstTile");

public Home(WebDriver driver) {
this.driver = driver;
// I've replaced my site with this fake url. The real site url works.

public void clickFirstTile() {

Answer Source

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.findElement(By.cssSelector("#wdp-tile a.product-link")).click();

You would edit a couple places in your classes.

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


public void clickFirstTile() {

I think that should take care of it.

More info on dealing with IFRAMEs.

How to switch between frames in Selenium WebDriver using Java

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download