2011-07-28 19 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

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を有効にすることである:最初の名前「Q」と要素の存在のために待機を追加することによって、あります名前が「q」の要素を見つけることができません。 'q'という名前の要素がありますか? – nitind

答えて

1

コードを実行すると同じ問題が発生します。

これはおそらく、JavaScriptがHtmlunitDriverでデフォルトで無効になっているためです。あなたのテストは、ページが完全にロードされる前に要素の位置を特定しようとしているようです。

HtmlUnitDriver wikiページの「Javascript in the HtmlUnitDriver」セクションで、これについて説明し、これがjavascriptとDOMの両方にどのように影響するかについて説明します。

私はあなたのコードを2通りの方法で実行させることができました。これは、「前記

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