所有:搜索使用硒的webdriver適用於谷歌,但未能對雅虎
我正在與硒的webdriver一個簡單的Java應用程序。
我能夠在http://www.google.com成功運行搜索 使用org.openqa.selenium.htmlunit.HtmlUnitDriver
我試圖在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();
Howefver,它給了我下面的錯誤:
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"));
當我們檢查輸入元素的名稱顯示'p'而不是'q'時。請嘗試使用'xpath或CSS比名稱定位器。讓我們知道。 – Sriram 2014-10-17 10:24:28
@ivan_ochc您是否爲了確定輸入元素標記名而點擊http://www.yahoo.com上的「查看源代碼」?如果是的話,你是如何在查看源代碼中找到所有混亂和混亂的元素標籤? – user1338998 2014-10-17 10:45:41
這是輸入元素的HTML。 。不是不歡迎使用名稱,而是使用xpath或CSS,建議使用 – Sriram 2014-10-17 11:24:01