0

All:

I am running a simple Java application with Selenium WebDriver.
I was able to successfully run a search on http://www.google.com using org.openqa.selenium.htmlunit.HtmlUnitDriver

I tried to run the same search term on http://www.yahoo.com as shown in the following code excerpt:

    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, it gives me the following error:

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)

Why does it work fine for http://www.google.com but fails for http://www.yahoo.com ?

Why does it throw the "Exception in thread main org.openqa.selenium.NoSuchElementException Unable to locate element with name q" error?

Update with Answer

Thanks to @Sriram and @ivan_ochc , I am able to run the following code that searches http://www.yahoo.com properly

    // 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"));
crazyTech
  • 1,379
  • 3
  • 32
  • 67
  • 2
    When we checked the name of input element showed 'p' rather 'q'. Please try using `xpath or CSS than name locator. Let us know. – Sriram Oct 17 '14 at 10:24
  • @ivan_ochc Did you both click on "View Source" for http://www.yahoo.com in order to determine the input element tag name? If yes, How in the world did you find the element tag in all that clutter and mess that shows up in View Source? – crazyTech Oct 17 '14 at 10:45
  • 2
    This is the HTML of input element. . Not that its unpopular to use name but xpath or CSS would be advisable – Sriram Oct 17 '14 at 11:24

1 Answers1

1

http://www.yahoo.com doesn't have element with name="q", it has element with name="p"

ivan_ochc
  • 392
  • 5
  • 22
  • 1
    @user1338998 In Chrome click on the right mouse button and select Inspect element. Firefox and other browsers also have such function. – ivan_ochc Oct 17 '14 at 11:25