2016-07-15 19 views
0

私はhtmlUnitDriverを使用してセレンの要素を見つけることができません。よくドライバが正常に動作しているが、私はGoogleの検索テキストボックスの要素を見つけることができません。ここでSelenium(htmlUnitDriver)の要素を見つけることができません

はコードです:ここでは

import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.htmlunit.HtmlUnitDriver; 

public class SampleUnitDriver 
{ 
    public static void main(String[] args) throws Exception 
    { 

      HtmlUnitDriver unitDriver = new HtmlUnitDriver(); 
      unitDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
      unitDriver.get("http://google.com"); 
      System.out.println("Title of the page is -> " + unitDriver.getTitle()); 

      WebElement searchBox = unitDriver.findElement(By.xpath(".//*[@id='gs_htif0']")); 
      searchBox.sendKeys("Selenium"); 
      WebElement button = unitDriver.findElement(By.name("gbqfba")); 
      button.click(); 
      System.out.println("Title of the page is -> " + unitDriver.getTitle()); 


    } 
} 

エラーです:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate a node using .//*[@id='gs_htif0'] For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: '2.53.0', revision: '35ae25b1534ae328c771e0856c93e187490ca824', time: '2016-03-15 10:43:46' System info: host: 'user-PC', ip: '192.168.1.52', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_51' Driver info: driver.version: SampleUnitDriver at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByXPath(HtmlUnitDriver.java:1165) at org.openqa.selenium.By$ByXPath.findElement(By.java:361) at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1725) at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1721) at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:1367) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1721) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:606) at com.digitalmqc.automation.action.SampleUnitDriver.main(SampleUnitDriver.java:19)

すべてのヘルプは理解することができます。

答えて

1

をあなたは間違った要素を見つけている、あなたが以下のように試してみてください: -

HtmlUnitDriver unitDriver = new HtmlUnitDriver(); 

unitDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
unitDriver.get("http://google.com"); 
System.out.println("Title of the page is -> " + unitDriver.getTitle()); 

WebElement searchBox = unitDriver.findElement(By.name("q")) 
searchBox.sendKeys("Selenium"); 
WebElement button = unitDriver.findElement(By.name("btnG")); 
button.click(); 
System.out.println("Title of the page is -> " + unitDriver.getTitle()); 

それがお役に立てば幸いです。.. :)

+0

ええ、動作@Saurabh :) –

0

要素を見つける前に、いくつかの明示的な待機の追加:

WebDriverWait wait = new WebDriverWait(driver, 10); 
WebElement searchBox = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='gs_htif0']")))); 
searchBox.sendKeys("Selenium"); 
+0

SRYこのエラーが発生しています:**スレッド "main"の例外org.openqa.selenium.TimeoutException:要素がクリック可能になるのを待つ10秒後にタイムアウトしました:By.xpath:.//*[@id='gs_htif0 ' ] ** –

+0

タイムアウトを20秒に変更し、xsspathが正しいことを確認してください。もしcssSelectorを使用しない場合は、ex: 'input [type = 'search']' –

+0

さんはCSSセレクターを使用しません。 : –

関連する問題