Get all Elements in a Form

混江龙づ霸主 提交于 2021-02-05 20:24:38

问题


I would like to use Selenium to submit a form which contains several elements. For example:

<form name="something">
    <input type="text" name="a">Username</input>
    <input type="password" name="b">password</input>
    <select name="c" id="c">
       <option value="1">1</option>
       <option value="2">2</option>
    </select>
    <input type="submit" name="submit">submit</input>
</form>

If I use find.Element(By.name) to find out the form element, how can I get its children elements a, b, and c? And input the values into these three elements then submit the form?

Another similar question is: if I get the element a, how to get elements b and c are in the same form and to fill (or select) values first, then submit the form?

Thanks in advance!


回答1:


You can use xpath to get all direct child elements of a specific element using parent/*.

If you already have your form element using findElement(), as below:

WebElement formElement = driver.findElement(By.name("something"));
List<WebElement> allFormChildElements = formElement.findElements(By.xpath("*"));

or directly using:

List<WebElement> allFormChildElements = driver.findElements(By.xpath("//form[@name='something']/*"));

Then look at the tag and type of each element to specify its value:

for (WebElement item : allFormChildElements)
{
    if (item.getTagName().equals("input"))
    {
        switch (item.getAttribute("type"))
        {
            case "text": 
                //specify text value
                break;
            case "checkbox":
                //check or uncheck
                break;
            //and so on
        }
    }
    else if (item.getTagName().equals("select"))
    {
        //select an item from the select list 
    }  
}



回答2:


driver = webdriver.Firefox()
driver.get("https://www.hackerearth.com/problems/")

#find all form input fields via form name
_inputs = driver.find_elements_by_xpath('//form[@name="signup-form"]//input')

for input in _inputs:                                                             
    #print attribute name of each input element 
    print input.get_attribute('name')

o/p
first_name
last_name
email
password
submit




回答3:


Sorry, missed the point of your question first. You can locate any element witihin the form with xpath locators, for example. In your case

find.Element(By.xpath("//form/*[@name='a']"))
find.Element(By.xpath("//form/*[@name='b']"))
find.Element(By.xpath("//form/*[@name='c']"))

If you have multiple form tags on your page, you can specify it with any attribute as well.

find.Element(By.xpath("//form[@name='something']/*[@name='c']")) //as it is in your sample

Also you can specify form first, and work with elements within it. I'm not sure abut your syntax, but first, you need to return the form webelement into some var (let it be form) in any way. After that you may pass this var instead of webdriver instance.

form.find.Element(By.xpath('./some/child/locator'))



回答4:


Store the form element in a variable, then use it as the search context to find the child elements:

WebElement formElement = driver.findElement(By.name("something"));
WebElement a = formElement.findElement(By.name("a"));
WebElement b = formElement.findElement(By.name("b"));
WebElement c = formElement.findElement(By.name("c"));

a.sendKeys("first child element [a]");
b.sendKeys("password");
c.submit();


来源:https://stackoverflow.com/questions/20148698/get-all-elements-in-a-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!