Handle Dynamic Google Search Using Dynamic XPath

Handle Dynamic Google Search Using Dynamic XPath

Handle Dynamic Google Search Using Dynamic XPath

import java.util.ArrayList;
import java.util.List;
 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
 
 
public class GoogleSearchTest {
 
 
public static void main(String[] args) throws InterruptedException {
 
System.setProperty("webdriver.chrome.driver", "C:\\batch230\\chromedriver.exe");
 
     WebDriver driver= new ChromeDriver();
 
     driver.get("https://www.google.com");
 
     Thread.sleep(5000);
 
     driver.manage().window().maximize();
 
    driver.findElement(By.name("q")).sendKeys("java tutorial");
 
   List<WebElement> list=driver.findElements(By.xpath("//ul[@role='listbox']//li[@class='sbct']"));
 
   System.out.println("Total number of suggestions in a search boxes are::===>" + list.size());
 
   for(int i=0; i<=list.size(); i++) {
   
   System.out.println(list.get(i).getText());
 
   if(list.get(i).getText().contains("java tutorial")){
 
   list.get(i).click();
 
   break;
 
   }
   }
}
 
}