2016-09-06 3 views
1

私はSeleniumとJavaの新機能ですが、Googleに読み込みを行い、検索を行い、結果の量と表示にかかる時間を表示する簡単なプログラムを作ろうとしていますgoogle検索。Googleの検索結果を表示するにはJavaとSeleniumの最良の方法

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' 
+0

?詳細を追加してください。 –

答えて

0

をそれはそれはresultStatsで、resultsStatsではありません。私は結果は私が取得していますされている結果との問題に実行して、ここで

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(); 


} 
} 

を表示することを取得しています。同じものを変更して、あなたは行くのが良いです!あなたが直面している問題

wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("resultStats"))); 
driver.findElement(By.id("resultStats")); 

WebElement results = driver.findElement(By.id("resultStats")); //displays # of results from search 

resultStats

+0

これが役立つことを願っています。 –

+0

ありがとう!ダム構文エラー – colin

関連する問題