Selenium webdriver click google search

后端 未结 7 937
情话喂你
情话喂你 2021-02-01 07:06

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

相关标签:
7条回答
  • 2021-02-01 07:41
    public class GoogleSearch {
    
        public static void main(String[] args) {
    
            WebDriver driver=new FirefoxDriver();
            driver.get("http://www.google.com");
            driver.findElement(By.xpath("//input[@type='text']")).sendKeys("Cheese");   
            driver.findElement(By.xpath("//button[@name='btnG']")).click();
            driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); 
            driver.findElement(By.xpath("(//h3[@class='r']/a)[3]")).click();
            driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); 
        }
    }
    
    0 讨论(0)
  • 2021-02-01 07:45
    @Test
    public void google_Search()
    {
        WebDriver driver;
        driver = new FirefoxDriver();
        driver.get("http://www.google.com");
        driver.manage().window().maximize();
    
        WebElement element = driver.findElement(By.name("q"));
        element.sendKeys("Cheese!\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"));
    
        //Get the url of third link and navigate to it
        String third_link = findElements.get(2).getAttribute("href");
        driver.navigate().to(third_link);
    }
    
    0 讨论(0)
  • 2021-02-01 07:53

    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/
    0 讨论(0)
  • 2021-02-01 07:57

    Based on quick inspection of google web, this would be CSS path to links in page list

    ol[id="rso"] h3[class="r"] a

    So you should do something like

    String path = "ol[id='rso'] h3[class='r'] a";
    driver.findElements(By.cssSelector(path)).get(2).click();
    

    However you could also use xpath which is not really recommended as a best practice and also JQuery locators but I am not sure if you can use them aynywhere else except inArquillian Graphene

    0 讨论(0)
  • 2021-02-01 08:02

    There would be multiple ways to find an element (in your case the third Google Search result).

    One of the ways would be using Xpath

    #For the 3rd Link
    driver.findElement(By.xpath(".//*[@id='rso']/li[3]/div/h3/a")).click();
    #For the 1st Link
    driver.findElement(By.xpath(".//*[@id='rso']/li[2]/div/h3/a")).click();
    #For the 2nd Link
    driver.findElement(By.xpath(".//*[@id='rso']/li[1]/div/h3/a")).click();
    

    The other options are

    By.ByClassName 
    By.ByCssSelector 
    By.ById
    By.ByLinkText
    By.ByName
    By.ByPartialLinkText 
    By.ByTagName 
    

    To better understand each one of them, you should try learning Selenium on something simpler than the Google Search Result page.

    Example - http://www.google.com/intl/gu/contact/

    To Interact with the Text input field with the placeholder "How can we help? Ask here." You could do it this way -

    # By.ByClassName 
    driver.findElement(By.ClassName("searchbox")).sendKeys("Hey!");
    # By.ByCssSelector 
    driver.findElement(By.CssSelector(".searchbox")).sendKeys("Hey!");
    # By.ById
    driver.findElement(By.Id("query")).sendKeys("Hey!");
    # By.ByName
    driver.findElement(By.Name("query")).sendKeys("Hey!");
    # By.ByXpath
    driver.findElement(By.xpath(".//*[@id='query']")).sendKeys("Hey!");
    
    0 讨论(0)
  • 2021-02-01 08:05

    Simple Xpath for locating Google search box is: Xpath=//span[text()='Google Search']

    0 讨论(0)
提交回复
热议问题