1

This is my test class with all the lines of code. I think the issue is in the xpath because of that it is not able to find the elements.

package practice;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class selPractice {

public static void main(String[] args) throws InterruptedException
{
    String key="webdriver.chrome.driver";
    String value="./software/chromedriver.exe";
    System.setProperty(key, value);
    WebDriver driver=new ChromeDriver();
    driver.get("https://www.google.com");
    //to automate auto suggestion 

driver.findElement(By.xpath("//input[@title='Search']")).sendKeys("motogp");
List<WebElement>motolist=driver.findElements(By.xpath("//ul[@role='listbox]
//li/descendant::div[@class='sbl1']"));
    Thread.sleep(2000);

    int count=motolist.size();
    System.out.println(count);
    for(WebElement list:motolist)
    {
        String text=list.getText();
        System.out.println(text);
    }
}
}
DebanjanB
  • 118,661
  • 30
  • 168
  • 217

1 Answers1

2

To extract the Auto Suggestions from the Search Box on Google Home Page you have to induce WebDriverWait for the visibilityOfAllElements and you can use the following solution:

  • Code Block:

    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;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class Google_Auto_Suggestions {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("start-maximized");
            options.addArguments("disable-infobars"); 
            options.addArguments("--disable-extensions"); 
            WebDriver driver = new ChromeDriver(options);
            driver.get("http://www.google.com");
            new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.name("q"))).sendKeys("motogp");
            List<WebElement> motolist = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//form[@action='/search' and @role='search']//ul[@role='listbox']//li//span")));
            for(WebElement list:motolist)
            {
                String text=list.getText();
                System.out.println(text);
            }
        }
    }
    
  • Console Output:

    Only local connections are allowed.
    Dec 04, 2018 6:14:51 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: OSS
    motogp
    motogp 2018
    motogp live
    motogp results
    motogp game
    motogp news
    motogp bikes
    motogp race
    motogp wiki
    motogp schedule
    
  • Browser Snapshot:

motogp_google_search_results

Here you can find a relevant discussion on Python Selenium Testing. How can I extract the Auto Suggestions from search box on Google Home Page?

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • Thanks a lot for your guidance,the code is working now . I have one doubt that every time we run our script there are multiple text boxes and various elements on which we have to perform actions like :typing,clicking,selecting etc so when to use WebDriver Wait and when to not? And what is preferable ImplicitWait or Explicitwait?? – Sumit Verma Dec 04 '18 at 13:12
  • okay i got your point but when to use the Explicitely wait i have this confusion because everytime in the script we have to perform actions like clicking,typing etc so should we use explicit wait everytime to wait for the element to be get loaded first. – Sumit Verma Dec 04 '18 at 13:26
  • @SumitVerma Implicitly Wait is no more effective and will die in the soon. So [Replace implicit wait with explicit wait](https://stackoverflow.com/questions/45712431/replace-implicit-wait-with-explicit-wait-selenium-webdriver-java/45715759#45715759). Specifically, whenever there is a change in the [HTML DOM](https://www.w3schools.com/js/js_htmldom.asp) before you interact with any of the element you should be using [ExplicitWait](https://stackoverflow.com/questions/49749269/java-wrapper-method-for-waiting-for-element-to-be-available-for-apache-cordova-w/49752577#49752577) – DebanjanB Dec 04 '18 at 13:27