Saturday, December 24, 2016

Lesson #9 - Selenium WebDriver - Finding element using the ID attribute (C#)

Overview

תמונה קשורה
The ID attribute is the most common and probably the most simplified way to locate web elements, in real automation projects, the developers will assign a unique ID for each web object that we can use to identify the element without the need to use more advanced and complex locators. 



In addition:
  • Elements should be set as static and not dynamic.
  • The ID uniqueness must be enforced by the developer.
  • Many Web Elements are not including such parameter. 

Code Example








Let's use the ID attribute to locate the "Name" field in the contact form

HTML
<input type="text" value="" size="30" name="name" id="ContactForm1_contact-form-name" class="contact-form-name">

C# Code 

[TestMethod]
public void FindElementById()
{
IWebElement WebElement;
IWebDriver Firefox = new FirefoxDriver();
Firefox.Navigate().GoToUrl("http://www.machtested.com/");
WebElement = Firefox.FindElement(By.Id("ContactForm1_contact-form-name"));
}
}

No comments:

Post a Comment

My Presentations