Paul Abraham
Paul Abraham

Reputation: 25

Unable to locate element link

I'm trying to locate 'Search' link, however I'm getting the following error: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"Search"}

I inspected the object using Firebug:

<li onclick="submitSelectedTab('tabSelected', 'TabGroup1', '12');" title="Search">
    <a href="#">
        <span>Search</span>
    </a>
</li>

The code I tried to use was

driver.findElement(By.linkText("Search")).click();

I've also tried: driver.findElement(By.partialLinkText("Search")).click();

I've also tried running the IDE which came back with: // ERROR: Caught exception [ERROR: Unsupported command [waitForPopUp | _blank | 30000]]

driver.findElement(By.cssSelector("li[title=\"Search\"] > a > span")).click();

The above code didn't work either.

I'm not sure if it makes a difference but to get to that page a new tab was loaded.

Upvotes: 1

Views: 401

Answers (2)

Maninder
Maninder

Reputation: 174

You can try with following code:

driver.findElement(By.xpath("//span[contains(text(), 'Search')]")).click();

Upvotes: 0

Guy
Guy

Reputation: 50809

You need to switch to the new tab

// get original tab handle
String currentHandle = driver.getWindowHandle();

// open the new tab here

// switch to the new tab
for (String handle : driver.getWindowHandles()) {
    if (!handle.equals(currentHandle))
    {
        driver.switchTo().window(handle);
    }
}

driver.findElement(By.linkText("Search")).click();

// close the new tab and switch back to the old tab
driver.close();
driver.switchTo().window(currentHandle);

Upvotes: 1

Related Questions