2016-07-15 138 views
0

我無法找到硒中的元素,我使用htmlUnitDriver。井司機工作正常,但我無法找到谷歌搜索文本框元素。無法在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基因好友我得到這個錯誤:**異常在線程「主要」org.openqa.selenium.TimeoutException:等待元素可點擊10秒後超時:By.xpath:.//*[@id='gs_htif0' ] ** –

+0

嘗試更改超時到20秒,並確保xpath是正確的,如果不嘗試使用cssSelector ex:'input [type ='search']' –

+0

Ohh man對CSS選擇器沒用,我給了20秒仍然沒有使用:( –

相關問題