Friday, February 17, 2017

Lesson #11 - Selenium WebDriver - Finding element using the Link/Partial Link text attribute (C#)

We can search elements using their link attribute, we can use either the full link of the element or only part of it.

In addition,
  • The first Link with this name will be used when executing the code (In case of duplications).
  • When the element is not found during the code execution, the method returns error type “NoSuchElementException”.


Code Example





To locate the contact form user name field, we will now use the "link Text" attribute:
<a href="http://www.machtested.com/p/blog-page_11.html">Quality Assurance</a>

[TestMethod]
public void FindElementByLinkText()
{
IWebElement WebElement;
IWebDriver Firefox = new FirefoxDriver();
Firefox.Navigate().GoToUrl("http://www.machtested.com/");
WebElement = Firefox.FindElement(By.LinkText("Quality Assurance"));
}

[TestMethod]
public void FindElementByPartialLinkText()
{
IWebElement WebElement;
IWebDriver Firefox = new FirefoxDriver();
Firefox.Navigate().GoToUrl("http://www.machtested.com/");
WebElement = Firefox.FindElement(By.PartialLinkText("Quality"));
}

No comments:

Post a Comment

My Presentations