简体   繁体   English

如何用selenium WebDriver找到文本字段的元素?

[英]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")); 请解释一下,是什么意思.findElement(By.name("q")); how do WD understand that it's text field? WD如何理解它的文本字段?

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

It is selecting an element with the value q for its name attribute. 它正在为其name属性选择值为q的元素。 It does not know that the element is an input it is only assigning it to the type WebElement . 它不知道元素是一个input ,只是将它分配给WebElement类型。

If you want to determine if it is an input you can call WebElement#getTagName and get its type via WebElement#getAttribute() 如果要确定它是否是input ,可以调用WebElement#getTagName并通过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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM