Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve the org.openqa.selenium.NoSuchElementException

Tags:

java

selenium

I am trying to run this program:

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class HtmlDriver {
  public static void main(String[] args) {
     // 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.stumbleupon.com/home/");

      // Find the text input element by its name
     WebElement element = driver.findElement(By.name("q"));

      // Enter something to search for
      element.sendKeys("Cheese!");

      // 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.getPageSource());
 }
}

And I am getting the following exception:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element with name: q System info: os.name: 'Linux', os.arch: 'i386', os.version: '2.6.27-7-generic', java.version: '1.6.0_12' Driver info: driver.version: HtmlDriver at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByName(HtmlUnitDriver.java:651) at org.openqa.selenium.By$4.findElement(By.java:148) at org.openqa.selenium.htmlunit.HtmlUnitDriver$4.call(HtmlUnitDriver.java:1133) at org.openqa.selenium.htmlunit.HtmlUnitDriver$4.call(HtmlUnitDriver.java:1) at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:869) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1130) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:330) at com.webdrivertest.HtmlDriver.main(HtmlDriver.java:20)

Please help me out in resolving it.

like image 813
d5111 Avatar asked Jan 21 '23 19:01

d5111


2 Answers

There's no element with name="q" on that page, hence, NoSuchElementException. You took the example from google and changed the site it goes to, but it's still looking for a google search box on the page.

like image 72
Nick Avatar answered Feb 02 '23 18:02

Nick


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class Example  {
    public static void main(String[] args) {
        // Create a new instance of the html unit driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        try {
            WebDriver driver = new FirefoxDriver();

            // And now use this to visit Google
            driver.get("http://www.google.com");

            // Find the text input element by its name
            WebElement element = driver.findElement(By.name("q"));

            // Enter something to search for
            element.sendKeys("Cheese!");

            // 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());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

When change the HtmlUnitDriver to FirefoxDriver, it is work!

like image 40
erhun Avatar answered Feb 02 '23 19:02

erhun