-1

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)?

1

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.

0

Hope this help you:-

System.setProperty("webdriver.chrome.driver", "C:\\Work\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
driver.manage().window().maximize();
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("IBM");
Thread.sleep(1000);
WebElement matchingResult= driver.findElement(By.xpath(".//div[@class='aajZCb']/ul"));
List<WebElement> listResult= matchingResult.findElements(By.xpath("//li/div/div[@class='sbtc']"));
System.out.println(listResult.size());
//if you want to print matching results
     for(WebElement results: listResult)
       {
         String value= results.getText();
         System.out.println(value);
       }     
  • Thank you for the code but what I want is a bit different – Curious Coder Jun 22 at 11:57
  • when I type IBM before I hit enter I want to get the list of matches. when I type IBM in the search box it comes up with IBM, IBM stock, IBM Watson, IBM computer and IBM as the possible hints for the keyword. What is the way to capture all those possible hints. Thank you – Curious Coder Jun 22 at 12:00
  • Thank you for telling us @CuriousCoder, i have edited the code, hope this help you. – Upkar Singh Jun 24 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.