In your above example, By.ID and By.Name is the easiest way. You don't have to do anything special. (labels tend to have ID properties, so most likely the ID method will work)
Originally Posted by ranjith302
Let's say there wasn't an ID or unique name property. Generally, what I like to do is use CSS selectors because these are indexed by the browser and can be found quickly (ID is the quickest). The easiest way to add easy locators is just ask the developer, or if you can edit the source yourself, add extra css classes. Just agree with your developers on a prefixed name space and you can do things like this...
But if for some political reason you can't do that. You can use XPath DOM traversal. This gets very ugly and is very slow in terms of element acquisition speed, it's not preferred.
<input type="email" autofocus="" spellcheck="false" value="" placeholder="Enter your email" class="hidden automation-label-password" spellcheck="false" id="Passwd-hidden">
// and you can easily find this element by
var emailInput= driver.FindElementBy(By.CssSelector(".automation-label-password");
Here I'm selecting the label that contains the desired text, then I'm using ".." to pop up to the parent element, then selecting the first input field. This method, I'll reiterate is not preferred. It's very ugly and very slow. Other automation engineers will laugh at you if you write that code.
var emailInput= driver.FindElementBy(By.XPath("//label[contains(.,''Enter your email")]/../input");