Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium webdriver click google search

Tags:

java

selenium

I'm searching text "Cheese!" on google homepage and unsure how can I can click on the searched links after pressing the search button. For example I wanted to click the third link from top on search page then how can I find identify the link and click on it. My code so far:

package mypackage;

import org.openqa.selenium.By; 

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.chrome.ChromeDriver; 
import org.openqa.selenium.support.ui.WebDriverWait;

public class myclass {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "C:\\selenium-java-2.35.0\\chromedriver_win32_2.2\\chromedriver.exe");

         WebDriver driver = new ChromeDriver(); 
         driver.get("http://www.google.com"); 
         WebElement element = driver.findElement(By.name("q"));
         element.sendKeys("Cheese!");
         element.submit();

         //driver.close();
    }



}
like image 634
min2bro Avatar asked Aug 22 '13 17:08

min2bro


People also ask

How do I click on Google search results in Selenium Webdriver?

We can click on Google search with Selenium webdriver. First of all we need to identify the search edit box with help of any of the locators like id, class,name, xpath or css. Then we will input some text with the sendKeys() method.

How do you search Google using Selenium?

In order, to perform a search, we have to first land on the Google page with the help of the get method. Then identify the search edit box with the help of any of the locators like id, name, class, xpath, tagname, or css. Then enter the keyword in the search box with the help of the sendKeys method.


1 Answers

Google shrinks their css classes etc., so it is not easy to identify everything.

Also you have the problem that you have to "wait" until the site shows the result. I would do it like this:

public static void main(String[] args) {

    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.google.com");
    WebElement element = driver.findElement(By.name("q"));
    element.sendKeys("Cheese!\n"); // send also a "\n"
    element.submit();

    // wait until the google page shows the result
    WebElement myDynamicElement = (new WebDriverWait(driver, 10))
              .until(ExpectedConditions.presenceOfElementLocated(By.id("resultStats")));

    List<WebElement> findElements = driver.findElements(By.xpath("//*[@id='rso']//h3/a"));

    // this are all the links you like to visit
    for (WebElement webElement : findElements)
    {
        System.out.println(webElement.getAttribute("href"));
    }
}

This will print you:

  • http://de.wikipedia.org/wiki/Cheese
  • http://en.wikipedia.org/wiki/Cheese
  • http://www.dict.cc/englisch-deutsch/cheese.html
  • http://www.cheese.com/
  • http://projects.gnome.org/cheese/
  • http://wiki.ubuntuusers.de/Cheese
  • http://www.ilovecheese.com/
  • http://cheese.slowfood.it/
  • http://cheese.slowfood.it/en/
  • http://www.slowfood.de/termine/termine_international/cheese_2013/
like image 82
d0x Avatar answered Sep 19 '22 12:09

d0x