Below are the list of attribute that can use to find out WebElements.
Find Element By ID-
This will locates elements by the value of their id attribute. The link in the above example has an id that we can use:
<a id="change-password" class="btn" href="#">Forgotten Password</a>
Then, the link can be located using:
driver.findElement(By.id("change-password"));
If ID is available, then it should be used as the first preferred choice.
Find Element By Name
you have input fields which could be like:<input class="form-control" name="email" type="text" placeholder="Email"/>
We can then locate the email field by the input name attribute
driver.findElement(By.name("email"));
Find Element By Class Name
This locates elements by the value of the class attribute.<a id="change-password" class="btn" href="#">Forgotten Password</a>
Then, the link can be located using:
driver.findElement(By.className("btn"));
Find Element By XPath
XPath locators are mostly used. It requires knowledge in XPath query language.Let’s look at an example usage of an XPath for this HTML:
<a id="change-password" href="#">Change Password</a>
We can then locate the email field by the input name attribute
driver.findElement(By.xpath("//a[@id='change-password']"));
Find Element By CSS Selector
Locates elements via the driver’s underlying W3 CSS Selector engine. CSS selector locator is powerful as it can be used to locate any element on a page.<a id="change-password" class="btn" href="./some-link">Forgotten Password</a>
We can then locate the email field by the input name attribute
driver.findElement(By.cssSelector("#change-password"));
Here, # represents id of the element. And the . notation represents the class attribute of an element.
driver.findElement(By.cssSelector(".btn"));
Find Element By Link Text
This method locates elements by the exact text it displays. This method is normally the preferred locator for links on a page.For example, suppose we have this link on a page:
<a href="#" id="change-password" class="btn" >Forgotten Password</a>
Then, the link can be located using:
driver.findElement(By.linkText("Forgotten Password"));
Find Element By Partial Link Text
When we are not sure of the exact wording of the link text but want to find a link or links that contains a given text, we can usedriver.findElement(By.partialLinkText("Forgotten "));
OR
driver.findElement(By.partialLinkText("Password"));
Find Element By Tag Name
This locator finds elements by their HTML tag name.<h1>Welcome to SDET World!</h1>
We can then locate the email field by the input name attribute
driver.findElement(By.tagName("h1"));
No comments:
Post a Comment