RosAng
RosAng

Reputation: 1050

DOM element selection in WebDriver

I'm trying to run a small sample application using HtmlUnitDriver in Eclipse. My code is as follows:

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

public class TestHtmlUnitDriver {

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

But it is giving the following error (as displayed in the console of Eclipse):

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element with name: q
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_23'
Driver info: driver.version: TestHtmlUnitDriver
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByName(HtmlUnitDriver.java:714)
    at org.openqa.selenium.By$4.findElement(By.java:148)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1185)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:932)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1182)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:368)
    at com.comverse.plus.selenium.TestHtmlUnitDriver.main(TestHtmlUnitDriver.java:19)

Can someone help me resolve my problem?

Upvotes: 1

Views: 1920

Answers (2)

Iryna
Iryna

Reputation: 1

 import java.util.regex.Pattern;
 import java.util.concurrent.TimeUnit;
 import org.junit.*;
 import static org.junit.Assert.*;
 import static org.hamcrest.CoreMatchers.*;
 import org.openqa.selenium.*;
 import org.openqa.selenium.firefox.FirefoxDriver;
 import org.openqa.selenium.support.ui.Select;
 import org.openqa.selenium.support.ui.ExpectedCondition;
 import org.openqa.selenium.support.ui.WebDriverWait;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.WebElement;

 public class Selenium2Example  {

    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.com");

        WebElement element = driver.findElement(By.name("q"));
        element.sendKeys("Cheese!");
        element.submit();

        System.out.println("Page title is: " + driver.getTitle());

        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("cheese!");
            }
        });

        System.out.println("Page title is: " + driver.getTitle());
        TimeUnit.SECONDS.sleep(5);

        driver.quit();
    }
}

Upvotes: 0

Luiz Fernando Penkal
Luiz Fernando Penkal

Reputation: 1031

Running your code I got the same problem.

This is probably due to javascript being disabled by default in HtmlunitDriver. It seems that your test is trying to locate the element before the page has been completely loaded.

In the section "Javascript in the HtmlUnitDriver" of the HtmlUnitDriver wiki page they explain this and how this affects both javascript and the DOM.

I was able to make your code run in two ways. The first is by adding a wait for the presence of an element with the name "q":

    driver.get("http://www.google.com");

    Wait<WebDriver> wait = new WebDriverWait(driver, 5);
    ExpectedCondition<WebElement> condition =
        new ExpectedCondition<WebElement>() {
            @Override
            public WebElement apply(WebDriver driver) {
                return driver.findElement(By.name("q"));
            }
        };
    wait.until(condition);

    WebElement element = driver.findElement(By.name("q"));

The other is by enabling javascript after creating the HtmlUnitDriver:

    HtmlUnitDriver driver = new HtmlUnitDriver();
    driver.setJavascriptEnabled(true);

Upvotes: 1

Related Questions