Selenium webdriver click google search

后端 未结 7 936
情话喂你
情话喂你 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: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 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);
    }
    

提交回复
热议问题