2011-07-28 146 views
1

我試圖在Eclipse中使用HtmlUnitDriver來運行一個小的示例應用程序。我的代碼如下:WebDriver中的DOM元素選擇

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.htmlunit.HtmlUnitDriver; 

public class TestHtmlUnitDriver { 

    public static void main(String[] args) { 
     // Create a new instance of the html unit driver 
     // Notice that the remainder of the code relies on the interface, 
     // not the implementation. 

     WebDriver driver = new HtmlUnitDriver(); 

     // And now use this to visit Google 
     driver.get("http://www.google.com"); 

     // Find the text input element by its name 
     WebElement element = driver.findElement(By.name("q")); 

     // Enter something to search for 
     element.sendKeys("Cheese!"); 

     // Now submit the form. WebDriver will find the form for us from the element 
     element.submit(); 

     // Check the title of the page 
     System.out.println("Page title is: " + driver.getTitle()); 
    } 
} 

但它給下面的錯誤(如顯示在Eclipse的控制檯):

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element with name: q 
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_23' 
Driver info: driver.version: TestHtmlUnitDriver 
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByName(HtmlUnitDriver.java:714) 
    at org.openqa.selenium.By$4.findElement(By.java:148) 
    at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1185) 
    at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1) 
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:932) 
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1182) 
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:368) 
    at com.comverse.plus.selenium.TestHtmlUnitDriver.main(TestHtmlUnitDriver.java:19) 

有人可以幫助我解決我的問題?

+0

它說「無法找到名稱爲「q」的元素。是否有名爲'q'的元素? – nitind

回答

1

運行你的代碼我得到了同樣的問題。

這可能是由於HtmlunitDriver中的JavaScript被默認禁用。看起來你的測試正在試圖在頁面被完全加載之前找到元素。

在HtmlUnitDriver wiki頁面的「HtmlUnitDriver中的Javascript」一節中,他們解釋了這個以及它如何影響javascript和DOM。

我能夠讓你的代碼以兩種方式運行。第一種是通過將一個元素的存在等待的名稱爲「Q」:

driver.get("http://www.google.com"); 

    Wait<WebDriver> wait = new WebDriverWait(driver, 5); 
    ExpectedCondition<WebElement> condition = 
     new ExpectedCondition<WebElement>() { 
      @Override 
      public WebElement apply(WebDriver driver) { 
       return driver.findElement(By.name("q")); 
      } 
     }; 
    wait.until(condition); 

    WebElement element = driver.findElement(By.name("q")); 

另一種是通過創建HtmlUnitDriver後啓用JavaScript:

HtmlUnitDriver driver = new HtmlUnitDriver(); 
    driver.setJavascriptEnabled(true); 
0
import java.util.regex.Pattern; 
import java.util.concurrent.TimeUnit; 
import org.junit.*; 
import static org.junit.Assert.*; 
import static org.hamcrest.CoreMatchers.*; 
import org.openqa.selenium.*; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.support.ui.Select; 
import org.openqa.selenium.support.ui.ExpectedCondition; 
import org.openqa.selenium.support.ui.WebDriverWait; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 

public class Selenium2Example { 

    public static void main(String[] args) throws InterruptedException { 
     WebDriver driver = new FirefoxDriver(); 
     driver.get("http://www.google.com"); 

     WebElement element = driver.findElement(By.name("q")); 
     element.sendKeys("Cheese!"); 
     element.submit(); 

     System.out.println("Page title is: " + driver.getTitle()); 

     (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { 
      public Boolean apply(WebDriver d) { 
       return d.getTitle().toLowerCase().startsWith("cheese!"); 
      } 
     }); 

     System.out.println("Page title is: " + driver.getTitle()); 
     TimeUnit.SECONDS.sleep(5); 

     driver.quit(); 
    } 
}