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!
提前致谢!
4 个解决方案
#1
14
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:
如果您已经使用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
}
}
#2
3
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
submito / p first_name last_name电子邮件密码提交
#3
0
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.
您还可以先指定表单,然后使用其中的元素。我不确定你的语法,但首先,你需要以任何方式将形式webelement返回到var(让它成为形式)。之后,您可以传递此var而不是webdriver实例。
form.find.Element(By.xpath('./some/child/locator'))
#4
0
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();
#1
14
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:
如果您已经使用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
}
}
#2
3
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
submito / p first_name last_name电子邮件密码提交
#3
0
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.
您还可以先指定表单,然后使用其中的元素。我不确定你的语法,但首先,你需要以任何方式将形式webelement返回到var(让它成为形式)。之后,您可以传递此var而不是webdriver实例。
form.find.Element(By.xpath('./some/child/locator'))
#4
0
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();