Google+

430. Using findElements( )







Till now we have worked with  findElement( ) command to locate Web Elements. But now we are going to work with findElements( ) command.

What is the difference between findElement( ) and findElements( ) commands ?

findElement( ) is for locating a single WebElement, where as findElements( ) can be used for locating multiple WebElements.

Example:

Using findElement( ) you can only locate a single link on a page. But if we have to locate all the links on the page, we have to use findElements( ) for locating instead of findElement( ).

Example Code:


//Locate all the links on the page using findElements(By.tagName("a"))
//Assing the located elements to the List<WebElement> type variable List<WebElement>  homepage_links = _driver.findElements(By.tagName("a"));


Test Description:

Count all the links on http://book.theautomatedtester.co.uk/ page as shown in the below screen:



Lets Implement the Test on Eclipse IDE:

Pre-requisites:

1. Create a new Java Project say 'WebDriver-Project74' in Eclipse IDE
2. Configure the Project to work with Selenium WebDriver
3. Create a package say 'package74' under the newly created project.
4. Create a Java Class file say 'Class74' under the newly created package as shown below:




Actual Steps:

1. Write the following code into the newly created Java Class file as shown below and make sure that you resolve all the errors before going to next step:




2. Create a test method 'countLinksOnHomePage( )'  as shown below:



3.  Locate all the link elements on the page using findElements(By.tagName("a")) and assign the located elements to List<WebElement> type variable as shown below:



4. Count the number of links available on the Home page by writing a for loop as shown below:



5. Enter the number of links count into the text area field as shown below:




6. Save and Run the 'Class74.java' file by selecting the 'JUnit Test' option and ensure that our Automation Test has located all the links on the page, counted all the located links and entered the links count into a text area field as shown below:



Watch the below video:

Click here to watch the video.

Download this Project:


Click here to download this project and import into Eclipse IDE  on your machine.






Please comment below to feedback or ask questions.

Using getTitle( ) to retrieve the title of the page  will be explained in the next post.



No comments:

Followers :)

Blog Index