Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java selenium用于谷歌搜索_Java_Xpath_Selenium_Google Search - Fatal编程技术网

Java selenium用于谷歌搜索

Java selenium用于谷歌搜索,java,xpath,selenium,google-search,Java,Xpath,Selenium,Google Search,我正在尝试用selenium构建一个简单的函数,给定一个文本可以从Google返回所有结果。我使用xpath查找所有结果元素。尽管“//a[@class='l']”在firebug中用作合法的xpath,但当我运行代码时,结果列表是空的 public void testSearch() { WebDriver driver = new FirefoxDriver(); driver.get("http://www.google.com"); WebElement que

我正在尝试用selenium构建一个简单的函数,给定一个文本可以从Google返回所有结果。我使用xpath查找所有结果元素。尽管“//a[@class='l']”在firebug中用作合法的xpath,但当我运行代码时,结果列表是空的

public void testSearch() {
    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.google.com");

    WebElement query = driver.findElement(By.name("q"));
    query.sendKeys("obama twitter");
    query.submit();

    long end = System.currentTimeMillis() + 5000;
    while (System.currentTimeMillis() < end) {
        WebElement resultsDiv = driver.findElement(By.className("gssb_e"));

        if (resultsDiv.isDisplayed()) {
            break;
        }
    }

    List<WebElement> weblinks = driver.findElements(By.xpath("//a[@class='l']"));
    for (WebElement suggestion : weblinks) {
        System.out.println(suggestion.getText()+"\n");
        System.out.println("==> "+suggestion.getAttribute("href")+"\n");
    }
}
public void testSearch(){
WebDriver=newfirefoxdriver();
驱动程序。获取(“http://www.google.com");
WebElement query=driver.findElement(By.name(“q”));
sendKeys(“奥巴马推特”);
query.submit();
长端=System.currentTimeMillis()+5000;
while(System.currentTimeMillis()”+suggestion.getAttribute(“href”)+“\n”);
}
}

我尝试了相同的代码,它正在获取页面上所有可用的链接。请检查以下输出。巴拉克·奥巴马(BarackObama)在推特上==>生日快乐…==>再过四年==>奥巴马的“绝地心灵融合”失言在推特上激起了笑话==>同意海姆切的观点。我无法复制你的问题。这就是说,你的等待技巧很粗糙,很可能是你问题的根源。看一看,然后等待。与他们一起玩,看看与他们或网络相关的问题-你需要掌握这些才能有效地使用WebDriver。