Friday, February 17, 2017

Lesson #10 - Selenium WebDriver - Finding element using the Name attribute (C#)

Finding element using the Name attribute

תמונה קשורהAlthough it's sims very logical to find am element by his name, there are few major limitations that you need to know:

·      The name attribute may not be unique on a page (the same name can be assigned to multiple elements).
·      If you use this attribute and it's assigned to multiple elements, the first element on the page with this name will be returned by the API.
Code Example
To locate the contact form user name field, we will now use the name attribute:

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

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

No comments:

Post a Comment

My Presentations