使用Selenium进行搜索WebDriver适用于谷歌,但在雅虎上失败

时间:2014-10-17 10:09:33

标签: java selenium selenium-webdriver htmlunit-driver

所有

我正在使用Selenium 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&#34的元素;错误?

使用答案进行更新

感谢@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"));

1 个答案:

答案 0 :(得分:1)

http://www.yahoo.com没有名称=" q"的元素,它的元素名称为" p"