So far we learned about identifying and interacting with a single element in web page. What if we need to interact with multiple elements in a web page? Let’s modify our first test case and create a new one where we will interact with multiple web elements:
Second Test Case:
Navigate to “Google” website using Chrome Browser. Find the Search Web Element, enter a text “Selenium” and then do a search. Click on the second link which contains the text ‘Wikipedia’ and then verify the title of the web page displayed.
As you see that our requirement got changed. We need to click on the second link having with the text ‘Wikipedia’. So how do we overcome this problem?
One of the solution to this problem is to get a list of web elements using the ‘Partial Link Text‘ locator and store the elements in a List. From the List (Collections in JAVA), get the correct web element and then click on it to navigate to the required web page. Now let’s see how we can create a script in Selenium for our second test case.
package com.selenium.firstscript; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class FifthScript { public static void main(String[] args) throws InterruptedException { // Setting System properties System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "/Firefox/geckodriver"); // Initialize WebDriver WebDriver driver = new FirefoxDriver(); // Navigate to Google Website driver.get("http://www.google.com"); //Maximize browser window driver.manage().window().maximize(); //Relative XPath for the text box driver.findElement(By.xpath("//input[@id='lst-ib']")).sendKeys("selenium"); //Find the Web Element Search icon. After finding, click the search icon driver.findElement(By.id("_fZl")).click(); //Wait for page to load. Thread.sleep() throws InterruptedException Thread.sleep(3000); //Locate all elements that contains the text Wikipedia and store them to a List List listSelenium = driver.findElements(By.partialLinkText("Wikipedia")); //verify the size of the List to confirm if the size is greater than 0 System.out.println("Size of listSelenium: "+listSelenium.size()); /*Now to get the second element from the List and click on it. Notice the index is 1 as the elements in a List starts with 0 index*/ listSelenium.get(1).click(); //Store the title in a variable String title = driver.getTitle(); //Title verification. The third link is Wikipedia link if (title.equals("Selenium (software) - Wikipedia")){ System.out.println("TITLE VERIFIED."); System.out.println("TEST CASE PASSED"); } else System.out.println("TITLE NOT VERIFIED."); //Close and quit the driver to close the Browser driver.close(); driver.quit(); } }
Since we are expecting more than one elements to be returned, we have to use findElements in stead of findElement as displayed below.
//Locate all elements that starts with the text Selenium and store it in a List List listSelenium = driver.findElements(By.partialLinkText("Wikipedia"));
The above code runs successfully in Eclipse and you can verify the results in the console.
With this we came to end on one of the biggest topics in Selenium testing. Keep on reading and I will update soon with new interesting topics. Till then, Happy Learning!