How Do I iterate through all the search results in Selenium returned by google search



  • My test steps are-

    • a) go to google.com
    • b) in the search box start typing a keyword example "IBM"
    • c) google displays all the matching results
    • d) Get the count of matching results in a List of strings.

    for (a), (b) I have:-

    System.setProperty("webdriver.chrome.driver", "C:\\Work\\chromedriver.exe");
            WebDriver driver = new ChromeDriver();
            driver.get("http://www.google.com");
            WebElement element = driver.findElement(By.name("q"));
            element.sendKeys("IBM");
    

    The Question is how I do (d)?



  • I have checked the first two results and the locator is same.

    So you can create a List of these elements by using FindElements method.

    And then put logic to create list of only matching Strings out of it.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2