全て:
Selenium WebDriver を使用して単純な Java アプリケーションを実行しています。org.openqa.selenium.htmlunit.HtmlUnitDriver を使用してhttp://www.google.com
で検索を正常に実行できました
次のコードの抜粋に示すように、http://www.yahoo.comで同じ検索用語を実行しようとしました。
CharSequence[] searchTerm = { "bbc", "news" };
// 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.yahoo.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
//searchTerm = "bbc news";
// Enter something to search for
element.sendKeys(searchTerm);
// 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());
driver.quit();
ただし、次のエラーが表示されます。
Oct 17, 2014 3:18:44 PM org.apache.http.client.protocol.ResponseProcessCookies processCookies
WARNING: Cookie rejected [DNR="deleted", version:0, domain:.www.yahoo.com, path:/, expiry:Thu Oct 17 15:18:45 IST 2013] Illegal domain attribute "www.yahoo.com". Domain of origin: "in.yahoo.com"
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.43.1', revision: '5163bce', time: '2014-09-10 16:27:58'
System info: host: , ip: , os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_05'
Driver info: driver.version: HtmlUnitDriver
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByName(HtmlUnitDriver.java:1001)
http://www.google.comでは問題なく動作するのに、 http://www.yahoo.comでは失敗するのはなぜ ですか?
「スレッド メイン org.openqa.selenium.NoSuchElementException で例外が発生しました。q という名前の要素が見つかりません」というエラーがスローされるのはなぜですか?
回答で更新
@Sriram と @ivan_ochc のおかげで、http://www.yahoo.com を適切に検索する次のコードを実行できます。
// 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.yahoo.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("p"));