Rudziankoŭ
Rudziankoŭ

Reputation: 11251

How to find element of textfield with selenium WebDriver?

I have already an example with google. Explain me please, what means .findElement(By.name("q")); how do WD understand that it's text field?

 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"));

Upvotes: 1

Views: 2574

Answers (1)

Kevin Bowersox
Kevin Bowersox

Reputation: 94429

It is selecting an element with the value q for its name attribute. It does not know that the element is an input it is only assigning it to the type WebElement.

If you want to determine if it is an input you can call WebElement#getTagName and get its type via WebElement#getAttribute()

Example

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"));

if (element.getTagName().equalsIgnoreCase("input") 
        && element.getAttribute("type").equalsIgnoreCase("text")) {
    System.out.println("its a textbox");
}

Upvotes: 3

Related Questions