Unable to Select the auto suggestions in google search text box

933 views
Skip to first unread message

nikhil rao

unread,
Feb 29, 2016, 8:13:18 AM2/29/16
to webdriver
1. I'm opening the google.com
2. I'm providing the search Selenium
3. Now I'm trying to print all the suggestions but text itself is not being entered in Text box


see code below:

WebDriver driver = new FirefoxDriver();
            driver.get("https://www.google.co.in");
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
            driver.findElement(By.xpath("//div[@id='sb_ifc0']")).sendKeys("Selenium");
            String xpath="//div/ul[@class='sbsb_b']/li[2]";
            List<WebElement> L1=driver.findElements(By.xpath(xpath));
            int iSize=L1.size();
            //L1.get(0).click();
            for (int i=0;i<iSize;i++)
            {
                String Link_Text=L1.get(i).getText();
                System.out.println(Link_Text);

darrell grainger

unread,
Mar 2, 2016, 11:40:56 AM3/2/16
to webdriver
The findElement call you are doing is finding a DIV element. You do not, normally, send text to a DIV element. You really want to find the INPUT element and send the string "Selenium" to that INPUT element. Once you send the text to the correct element then you want to find the list of suggestions. I like to use CSS selector because it is a little less verbose than the XPath locator. Additionally, I would use WebDriverWait to wait for the suggestion list to be populated. So, you are close with your code but I would have implemented it using:

WebDriver driver = new ChromeDriver(); // create an instance of the browser
WebDriverWait wdw = new WebDriverWait(driver, 30); // create something which can wait for the suggestions
driver.manage().window().maximize(); // maximize the browser window
driver.get("https://www.google.co.in/"); // go to Google Search
driver.findElement(By.cssSelector("#lst-ib")).sendKeys("Selenium"); // send the string "Selenium" to the input
wdw.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector(".sbsb_b .sbqs_c"))); // wait for the suggestions
List<WebElement> suggestions = driver.findElements(By.cssSelector(".sbsb_b .sbqs_c")); // find all the suggestions
for(WebElement suggestion : suggestions) { // iterate over all the suggestions
System.out.println(suggestion.getText()); // print the suggestions
}
driver.quit(); // close the browser

Notice that computers are stupid. You REALLY have to spell out everything for the computer:
  1. Create an instance of the browser
  2. Create something to wait for the suggestions
  3. Maximize the browser window
  4. Go to Google Search
  5. Send the string "Selenium" to the input
  6. Wait for the suggestions
  7. Find all the suggestions
  8. Iterate over all the suggestions
  9. Print the suggestions
  10. Close the browser
You can clean this up a little. I have the string ".sbsb_b .sbqs_c" used twice plus it is not clear what this is referring to. So I might change this to:

By googleSearchSuggestionCSSSelector = By.cssSelector(".sbsb_b .sbqs_c");
 
then I can use this variable in the code. If you did want to use XPath rather than a CSS selector you could use:

By googleSeaarchSuggestionXPathLocator = By.xpath("//*[contains(@class, 'sbsb_b')]//*[contains(@class,'sbqs_c')]");

This XPath locator is identical to the CSS selector.
Reply all
Reply to author
Forward
0 new messages