johnnewbie25
johnnewbie25

Reputation: 149

How to use Selenium to grab nested LinkText?

I'm using Selenium in C# to click on a link called 'Store Locator'. My current code is as follows but does not click on the link:

IWebElement storeLink = driver.FindElement(By.LinkText("Store Locator"));

Here is the original HTML, notice there is a span element nested in the tag, not sure if this makes a difference.

<a href="/site/olspage.jsp?id=cat12090&amp;type=page&amp;rdct=n" data-lid="hdr_stl"><span class="header-icon-storeFinder" aria-hidden="true"></span>Store Locator</a>

Upvotes: 0

Views: 1552

Answers (2)

Dinesh babu
Dinesh babu

Reputation: 31

This will work for sure.

IWebElement storeLink = driver.FindElement(By.LinkText("Store Locator"));
storeLink.click();

Upvotes: 0

Saifur
Saifur

Reputation: 16201

Seems like the link is hidden. If even Selenium finds the link it will not be able to interact directly. In that case JavaScript is your only option

By xpath = By.XPath("//span[contains(text(),'Store Locator')]");
IWebelement element = driver.FindElement(xpath);
((IJavaScriptExecutor)driver).ExecuteScript(@"arguments[0].click();",element);

Upvotes: 1

Related Questions