HtmlUnitDriver を開発環境で動作させようとしています。初心者として、最新の Selenium サーバー jar を使用して次のページの例を実装してみました: http://code.google.com/p/selenium/wiki/GettingStarted
残念ながら、このプログラムを実行しようとすると、次の例外が発生します。
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element with name: q
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.35.0', revision: 'c916b9d', time: '2013-08-12 15:42:01'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_16'
Driver info: driver.version: HtmlUnitDriver
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByName(HtmlUnitDriver.java:853)
at org.openqa.selenium.By$ByName.findElement(By.java:292)
at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1404)
at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:1094)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1401)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:419)
at Justin.Main.main(Main.java:30)
ここに実装された修正を組み込むためにコードを変更しようとしました:
URL の取得中に HtmlUnitDriver が問題を引き起こす
driver.getCurrentUrl()
の呼び出し後にを使用してページの URL を取得しようとしましたdriver.get("http://www.google.com")
が、返される文字列はabout:blank
.
この例の同様のコードは、FirefoxDriver を使用して実行すると確実に機能しますが、要件を満たすには、スクリプトをセレンでヘッドレスで実行する必要があります (ヘッドレスである限り、特定の BrowserVersion で実行してもかまいません)。 .
どんな助けでも大歓迎です。
アップデート:
これは私が今実行しようとしているコードです。HtmlUnitDriver を、Google に検索クエリを入力するだけの簡単なことで動作させることができることを確認したいだけです。
package Justin;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Main {
public static void main(final 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.
final WebDriver driver = new HtmlUnitDriver();
((HtmlUnitDriver)driver).setJavascriptEnabled(true);
// And now use this to visit Google
driver.get("http://www.google.com");
try {
Thread.sleep(30000);
} catch (final InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Find the text input element by its name
final 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());
}
}
更新 2:
この問題は非常に奇妙です。自宅のコンピューターで同じコードを試してみましたが、BrowserVersion を Firefox または Internet Explorer に設定すると問題なく動作します。この問題は間違いなく私の職場のコンピューター構成によって引き起こされていますが、なぜこれが起こっているのかはまだわかりません.