2014-10-17 31 views
0

所有:搜索使用硒的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")); 
+2

当我们检查输入元素的名称显示'p'而不是'q'时。请尝试使用'xpath或CSS比名称定位器。让我们知道。 – Sriram 2014-10-17 10:24:28

+0

@ivan_ochc您是否为了确定输入元素标记名而点击http://www.yahoo.com上的“查看源代码”?如果是的话,你是如何在查看源代码中找到所有混乱和混乱的元素标签? – user1338998 2014-10-17 10:45:41

+2

这是输入元素的HTML。 。不是不欢迎使用名称,而是使用xpath或CSS,建议使用 – Sriram 2014-10-17 11:24:01

回答

1

http://www.yahoo.com没有元素与name =” q“,它具有名称=”p“的元素

+1

@ user1338998在Chrome中单击鼠标右键并选择检查元素。 Firefox和其他浏览器也有这样的功能。 – 2014-10-17 11:25:50