我想使用selenium webdriver打印所有Google自动建议

时间:2017-08-01 07:12:20

标签: java selenium-webdriver

我想使用selenium webdriver打印google自动建议列表。为此,我写的是:

System.setProperty("webdriver.chrome.driver", "D://JDK/ChromeDriver/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
driver.findElement(By.id("lst-ib")).sendKeys("selenium");

我输入了文字" Selenium",谷歌正在显示自动建议,我想了解所有建议,我想打印所有这些。 请指导。

4 个答案:

答案 0 :(得分:0)

这符合您的要求:

List<WebElement> print = driver.findElements(By.xpath("//div[@class='sbqs_c']"));
System.out.println(print.size());
for ( WebElement we: print) { 
    System.out.println(we.getText());
}

答案 1 :(得分:0)

    driver.get("https://www.google.co.in/");

    WebElement search = driver.findElement(By.xpath("//input[@class='gLFyf gsfi']"));
    search.sendKeys("java");

    //change class name as per requirement 
    List<WebElement> printlist = driver.findElements(By.xpath("//ul[@role='listbox']//li/descendant::div[@class='sbl1']"));

答案 2 :(得分:0)

试试这个

driver.get("http://www.google.com");
    WebElement query = driver.findElement(By.name("q"));

    query.sendKeys("Cheese");
    Thread.sleep(1000); // Wait for suggestions to be loaded
    List<WebElement> allSuggestions = driver
            .findElements(By.xpath("//ul[@role=\'listbox\']/li/div/div/div[@class=\'sbl1\']/span"));

    System.out.println("Total No. of Suggestions are: " + allSuggestions.size());

    for (WebElement suggestion : allSuggestions) {
        System.out.println(suggestion.getText());
    }

答案 3 :(得分:0)

driver.findElement(By.cssSelector("input.gLFyf.gsfi")).sendKeys("automation");
List<WebElement> suggestions=driver.findElements(By.cssSelector("ul>li.sbct"));
System.out.println(suggestions.size());
for(int i=0;i<suggestions.size();i++){
            System.out.println(i+" "+suggestions.get(i).getText());
}
driver.findElement(By.cssSelector("ul>li.sbct:nth-of-type(5)")).click();