Html 获取表单中的所有元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20148698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get all Elements in a Form
提问by Eve
I would like to use Selenium to submit a form which contains several elements. For example:
我想使用 Selenium 提交一个包含多个元素的表单。例如:
<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?
如果我使用 find.Element(By.name) 来找出表单元素,我怎样才能得到它的子元素 a、b 和 c?并将值输入这三个元素然后提交表单?
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?
另一个类似的问题是:如果我获取元素a
,如何获取元素b
并c
在相同的表单中并先填充(或选择)值,然后提交表单?
Thanks in advance!
提前致谢!
回答by Faiz
You can use xpath to get all direct child elements of a specific element using parent/*
.
您可以使用 xpath 获取特定元素的所有直接子元素parent/*
。
If you already have your form
element using findElement()
, as below:
如果您已经使用了您的form
元素findElement()
,如下所示:
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
}
}
回答by GrvTyagi
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
password
submit
o/p
first_name
last_name
电子邮件
密码
提交
回答by Furious Duck
Sorry, missed the point of your question first. You can locate any element witihin the form with xpath locators, for example. In your case
抱歉,首先错过了您的问题的重点。例如,您可以使用 xpath 定位器定位表单中的任何元素。在你的情况下
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
任何方式将表单 webelement 返回到某个 var(让它成为)。之后你可以传递这个 var 而不是 webdriver 实例。
form.find.Element(By.xpath('./some/child/locator'))
回答by Marielle
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();