您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Selenium Webdriver单击Google搜索

Selenium Webdriver单击Google搜索

Google缩小了CSS类等,因此识别所有内容并不容易。

另外,您还有一个问题,必须“等待”,直到站点显示结果为止。我会这样做:

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"));
    }
}

这将为您打印:

Go 2022/1/1 18:13:39 有747人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶