Selenium WebDriver : Difference Between findElement together with findElements alongside example

We bring seen many examples of webdriver's findElement() inwards my previous posts. You volition uncovering syntax of findElement() amongst instance on THIS PAGE if y'all wants to utilisation it inwards your software spider web application assay case. Selenium WebDriver software testing tool has 1 to a greater extent than related method findElements. Now primary enquiry is what is the difference between findElement and findElements methods inwards selenium ii software automation tool? First of all allow me supply y'all deviation description as well as therefore example.

findElement() method
  • We need to utilisation findElement method oftentimes inwards our webdriver software assay instance because this is the entirely means to locate whatsoever chemical component division inwards webdriver software testing tool.
  • findElement method is useful to locating targeted unmarried element.
  • If targeted chemical component division is non institute on the page therefore it volition throw NoSuchElementException.
findElements() method
  • We are using findElements method simply occasionally.
  • findElements method volition render listing of all the matching elements from electrical flow page every bit per given chemical component division locator mechanism.
  • If non institute whatsoever chemical component division on electrical flow page as per given chemical component division locator mechanism, it volition render empty list.
Now allow me supply y'all uncomplicated practical instance of findElement method as well as findElements method to exhibit y'all clear difference. You tin hand the axe VIEW ALL PRACTICAL EXAMPLES OF SELENIUM WEBDRIVER one past times one.

Copy bellow given @Test method utilisation of findElement as well as findElements method examples as well as supervene upon it amongst the @Test method utilisation of instance given on THIS PAGE(Note : @Test method is marked with pink color in that linked page).
@Test  populace void assay () throws InterruptedException   {    WebElement selection = driver.findElement(By.xpath("//option[@id='country5']"));   System.out.print(option.getAttribute("id")+" - "+option.getText());   List<WebElement> options= driver.findElements(By.xpath("//option"));   System.out.println(options.size());   for(int i=0;i<=options.size();i++)   {    String str = options.get(i).getAttribute("id")+" - "+options.get(i).getText();    System.out.println(str);       }     }

In inwards a higher house example, findElement will locate entirely targeted chemical component division as well as therefore impress its id as well as text inwards console while findElements volition locate all those elements of electrical flow page which are nether given xpath = //option. for loop is used to impress all those element's id as well as text inwards console.

Same means y'all tin hand the axe locate all input elements, link elements etc.. using findElements method.
More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post