At whatever point you intend to computerize any web application utilizing Selenium Webdriver, you generally begin by finding the HTML components on the page. At that point, you slope to pick the correct procedure to find them. Web-driver characterizes two strategies for recognizing the components: they are findElement and findElements.

Next, we’ll enable you to investigate the distinctive locator systems that you can use alongside these techniques.
One more point which you ought to obviously understand is the contrast between the findElement and findElements techniques. You can discover answers for every one of these focuses in this post.

 

1. The difference between findElement and findElements methods:

FindElement() Method

• You can utilize this order to get to any single component on the Web page.
• It returns the object of the first coordinating/matching element of the predetermined locator.
• It tosses a NoSuchElementException exemption when it fails to find the element.
• Sysntax is as follows:

 WebElement user = driver.findElement(By.id(“User”));

FindElements() Method

• First of everything, we don’t use it as often as possible as its use is exceptionally limited.
• It gives back the entire list of the considerable number of elements matching the predetermined locator.
• If the component doesn’t exist or not accessible on the page, the return value will be a empty list.Its syntax is as follows.
• List

linklist = driver.findElements(By.xpath(“//table/tr”));

2. Understand multiple By strategies to access locators:

By Id()
This is very easy way to find webpage element. Each and every element in your webpage has unique id assigned. Using that Id you can able to find Element.
For Ex :

 driver.findElement(By.id(“your element id”));

By Classname()
If you element contains classname then you can able to detect that element by classname also.
For Ex :

 driver.findElement(By.className(“classname”));

 By Name()
You can also find that element by their Name Property.
For Ex :

 driver.findElement(By.name(“Name of Element”));

By Xpath()
Xpath is very helpful when your two element having same id or name or class name and you want to find both element uniquely.
For Ex :

driver.findElement(By.xpath(“xpath of your element”));

By Cssselector()
You can also find the element by Selector. It is actually Css path of your Element.
For Ex :

driver.findElement(By.cssSelector(“your element selector”));

By Tagname()
If you want to find all element from page having tag , then you can use this method to find all elements. Tag can be , <a>,<input> etc.
For Ex :

driver.findElement(By.tagName(“a”));</a>


By Linktext()
You can find element by linktext , It means text which is having some link.
For Ex :

driver.findElement(By.linkText(“text which having link”));</a>


So above are simple and supportive routes by which you can find particular element from webpage.

Leave A Comment