我對Selenium和Java非常陌生,但我試圖製作一個簡單的程序,它可以加載谷歌,執行搜索,然後顯示多少結果以及加載需要多長時間谷歌搜索。我正在與結果的問題,並獲得該顯示Java和Selenium顯示Google搜索結果的最佳方式
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
public class GoogleSearch {
public static void main(String[] args) {
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://google.com");
WebElement searchTextBox = driver.findElement(By.id("lst-ib"));
searchTextBox.sendKeys("Colin");
String pageUrl = driver.getCurrentUrl();
System.out.println(pageUrl);
WebElement searchButton = driver.findElement(By.className("lsb"));
searchButton.click();
String pageTitle = driver.getTitle();
System.out.println("Page title is:" + pageTitle);
WebDriverWait wait = new WebDriverWait(driver, 5);// 5 seconds
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("resultsStats")));
driver.findElement(By.id("resultsStats"));
WebElement results = driver.findElement(By.id("resultsStats")); //displays # of results from search
System.out.println(results);
//driver.quit();
}
}
下面是我得到的結果:
https://www.google.com/?gws_rd=ssl
Page title is:Google
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"resultsStats"}
Command duration or timeout: 13 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:37:03'
哪些問題你面對?請添加更多信息。 –