{"id":2725,"date":"2020-04-22T18:19:10","date_gmt":"2020-04-22T12:49:10","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=2725"},"modified":"2024-09-25T16:50:16","modified_gmt":"2024-09-25T11:20:16","slug":"find-element-findelements-selenium-webdriver","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/find-element-findelements-selenium-webdriver\/","title":{"rendered":"Find Element and FindElements in Selenium WebDriver"},"content":{"rendered":"<h2><b>Need for Find Element and FindElements command?<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">A user is required to interact with a web page to locate the web element. The command Find Element is used to uniquely locate a web element within the web page. Whereas, the command FindElements is used to locate the list of web elements within the web page uniquely. There are many ways to identify a web element within the web page like Name, ID, Link Text, Partial Link Text, ClassName, XPath,\u00a0 and Tag Name.<\/span><\/p>\n<h2><b>Selenium FindElement Command:<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">FindElement command uses By object as a parameter and returns an object of type WebElement. By object can be used with different locator strategies like Name, ID, <a href=\"https:\/\/www.h2kinfosys.com\/blog\/interfaces-abstract-classes\/\">ClassName<\/a>, XPath, LinkText, etc.<\/span><\/p>\n<h3><b>Syntax of FindElement Command<\/b><\/h3>\n<p><b>WebElement elementName = driver.findElement(By.LocatorStrategy(\u201cLocatorValue\u201d));<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Following are the Locator Strategy values:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Name<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">ID<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Tag Name<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Class Name<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Link Text<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">XPath<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Partial Link Text<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Locator Value is the value through which a web element can be identified uniquely. It is the developer\u2019s and tester\u2019s job to make sure that web elements are identified uniquely with properties like Name or ID. <\/span><\/p>\n<p>[box type=&#8221;success&#8221; align=&#8221;&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]<\/p>\n<p><b>Example:<\/b><\/p>\n<p><b>WebElement login = driver.findElement(By.linkText(\u2018Login\u201d));\u00a0<\/b>[\/box]<\/p>\n<h2><b>Selenium FindElements Command:<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">FindElements command uses By object as the parameter and returns a list of <a href=\"https:\/\/www.h2kinfosys.com\/blog\/webdriver-script-java-example\/\">web elements<\/a>. It returns an empty list if no elements are found when given locator strategy and locator value.\u00a0<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">\u00a0<\/span><b>Syntax of FindElements Command<\/b><\/h3>\n<p><b>List&lt;WebElement&gt; elementName = \u00a0 \u00a0 driver.findElements(By.LocatorStragety(\u201cLocatorValue\u201d));<\/b><\/p>\n<p>[box type=&#8221;success&#8221; align=&#8221;&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]<\/p>\n<p><b>Example:<\/b><\/p>\n<p><b>List&lt;WebElement&gt; Elementslist = driver.findElements(By.xpath(\u201c\/\/div\u201d));<\/b><\/p>\n<p>[\/box]<\/p>\n<p>\u00a0<\/p>\n<h2><b>Difference between findElement and find Elements Methods<\/b><\/h2>\n<table>\n<tbody>\n<tr>\n<td><b>findElement<\/b><\/td>\n<td><b>findElements<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Return single element\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Returns a list of elements<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">It returns only first element when multiple elements are found using the same locator<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Return all elements from a list of web elements<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">If no matching element found, throw NoSuchElementFound Exception<\/span><\/td>\n<td><span style=\"font-weight: 400;\">If no matching element found, then it will return an empty list<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Return single element<\/span><\/td>\n<td><span style=\"font-weight: 400;\">To get the element from the list, we need to iterate over it<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Apply to find element method over element found<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Not possible to apply<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Not Applicable<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Each Web Element is indexed with 0 number just like an array<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<h2><b>How to use Selenium findElement Command<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">The below application is used for demo purpose:<\/span><\/p>\n<p><b>https:\/\/www.facebook.com\/<\/b><b>\u00a0<\/b><\/p>\n<p><b>Scenario:<\/b><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Open the <\/span><span style=\"font-weight: 400;\">https:\/\/www.facebook.com\/<\/span><span style=\"font-weight: 400;\"> for AUT<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Find and click the radio button<\/span><\/li>\n<\/ol>\n<pre><b>package<\/b><span style=\"font-weight: 400;\">\u00a0 com.testing.selenium.findelement;<\/span>\n\n<b>import<\/b><span style=\"font-weight: 400;\"> org.openqa.selenium.By;<\/span>\n<b>import<\/b><span style=\"font-weight: 400;\"> org.openqa.selenium.WebDriver;<\/span>\n<b>import<\/b><span style=\"font-weight: 400;\"> org.openqa.selenium.chrome.ChromeDriver;<\/span>\n\n<b>public<\/b> <b>class<\/b><span style=\"font-weight: 400;\"> FindElement {<\/span>\n<b>public<\/b> <b>static<\/b> <b>void<\/b><span style=\"font-weight: 400;\"> main(String[] args) {<\/span>\n\n<span style=\"font-weight: 400;\">\/\/ TODO Auto-generated method stub<\/span>\n\n<span style=\"font-weight: 400;\">System.setProperty(\"webdriver.chrome.driver\", \"D:\\\\Drivers\\\\chromedriver.exe\");<\/span>\n\n<span style=\"font-weight: 400;\">WebDriver driver = <\/span><b>new<\/b><span style=\"font-weight: 400;\"> ChromeDriver();<\/span>\n\n<span style=\"font-weight: 400;\">driver.manage().window().maximize();<\/span>\n\n<span style=\"font-weight: 400;\">driver.get(\"https:\/\/www.facebook.com\/\");<\/span>\n\n<b>\/\/Find the radio button for Gender \"Male\" by using its ID and click on it<\/b>\n\n<span style=\"font-weight: 400;\">driver.findElement(By.id(\"u_0_7\")).click();<\/span>\n\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span>\n\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p>\u00a0<\/p>\n<h2><b>How to use Selenium find Elements Command<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">The below application is used for demo purpose:<\/span><\/p>\n<p><b>https:\/\/www.facebook.com\/<\/b><b>\u00a0<\/b><\/p>\n<p><b>Scenario:<\/b><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Open the <\/span><span style=\"font-weight: 400;\">https:\/\/www.facebook.com\/<\/span><span style=\"font-weight: 400;\"> for AUT<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Find the text of radio buttons and print on console<\/span><\/li>\n<\/ol>\n<pre><b>package<\/b><span style=\"font-weight: 400;\"> com.testing.selenium.findelements;<\/span>\n\n<b>import<\/b><span style=\"font-weight: 400;\"> java.util.List;<\/span>\n<b>import<\/b><span style=\"font-weight: 400;\"> org.openqa.selenium.By;<\/span>\n<b>import<\/b><span style=\"font-weight: 400;\"> org.openqa.selenium.WebDriver;<\/span>\n<b>import<\/b><span style=\"font-weight: 400;\"> org.openqa.selenium.WebElement;<\/span>\n<b>import<\/b><span style=\"font-weight: 400;\"> org.openqa.selenium.chrome.ChromeDriver;<\/span>\n<b>\npublic<\/b> <b>class <\/b><span style=\"font-weight: 400;\">\u00a0FindElements {<\/span>\n<b>public static<\/b><span style=\"font-weight: 400;\">\u00a0 <\/span><b>void<\/b><span style=\"font-weight: 400;\"> main(String[] args) {<\/span>\n\n<span style=\"font-weight: 400;\">\/\/ TODO Auto-generated method stub<\/span>\n\n<span style=\"font-weight: 400;\">System.setProperty(\"webdriver.chrome.driver\", \"D:\\\\Drivers\\\\chromedriver.exe\");<\/span>\n\n<span style=\"font-weight: 400;\">WebDriver driver =\u00a0 <\/span><b>new<\/b><span style=\"font-weight: 400;\"> ChromeDriver();<\/span>\n\n<span style=\"font-weight: 400;\">driver.manage().window().maximize();<\/span>\n\n<span style=\"font-weight: 400;\">driver.get(\"https:\/\/www.facebook.com\/\");<\/span>\n\n<b>\/\/Find the radio button for Gender \"Male\" by using its ID<\/b>\n\n<span style=\"font-weight: 400;\">List&lt;WebElement&gt; elements = driver.findElements(By.id(\"u_0_7\"));<\/span>\n\n<span style=\"font-weight: 400;\">System.out.println(\"Number of elements:\" +elements.size());<\/span>\n\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><b>for<\/b><span style=\"font-weight: 400;\">(<\/span><b>int<\/b><span style=\"font-weight: 400;\"> i=0;\u00a0 i&lt;elements.size();\u00a0 i++){<\/span>\n\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(\"Radio button text:\" + elements.get(i).getAttribute(\"value\"));<\/span>\n\n<span style=\"font-weight: 400;\"> \u00a0 \u00a0 \u00a0 }<\/span>\n\n<span style=\"font-weight: 400;\">\u00a0\u00a0}\n\n}<\/span><\/pre>\n<h2><b>Conclusion<\/b><\/h2>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The Find Element command returns a single element that matches the first element within the web page.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The Find Elements command returns a list of elements.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The Find Element command throws NoSuchElementException if no matching element is found.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The Find Elements command returns an empty list if no matching element is found, it will return an empty list.<\/span><\/li>\n<\/ul>\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-webdriver-junit-training-course\"><img fetchpriority=\"high\" decoding=\"async\" width=\"728\" height=\"90\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/11\/selenium-online-classes-here-1.jpeg\" alt=\"Selenium Classes Online\" class=\"wp-image-6596\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/11\/selenium-online-classes-here-1.jpeg 728w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/11\/selenium-online-classes-here-1-300x37.jpeg 300w\" sizes=\"(max-width: 728px) 100vw, 728px\" \/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Need for Find Element and FindElements command? A user is required to interact with a web page to locate the web element. The command Find Element is used to uniquely locate a web element within the web page. Whereas, the command FindElements is used to locate the list of web elements within the web page [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":2730,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[156,561,1835,1834,558,559,7,448,51,560],"class_list":["post-2725","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium-tutorials","tag-automation-testing","tag-difference-between-findelement-and-find-elements","tag-findelement-command","tag-selenium-find-elements","tag-selenium-findelement","tag-selenium-findelements","tag-selenium-quiz","tag-selenium-webdriver","tag-software-testing","tag-syntax-of-findelement"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2725","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=2725"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2725\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/2730"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=2725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=2725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=2725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}