我想在selenium中选择以下表单中的输入字段。我尝试了很多方法,但没有找到帮助,我的测试总是失败。 这是html表单:
`<form class="login" action="/Index/trylogin/?ru=L015RmlueWE=" novalidate>
<fieldset>
<label>
<input type="text" maxlength="14" tabindex="1" value="" placeholder="Pseudonym">
</label>
<label>
<input type="password" maxlength="20" tabindex="2" data-ch="8H7vtP9f9tns3TGMJ6F995kTyLSmwFsdDlIyBLhBBsrrW1ZHIZiec4cPqF7C5sp5" data-ch2="1782447a8c3759d4407ed522b831806e8cfde5dc" placeholder="Kennwort">
</label>
<input type="submit" value="Anmelden" class="button button-style1">
<div class="login-options">
<label>
<input type="checkbox" value="1" name="stayon">Automatisch anmelden (auf diesem Gerät)
</label>
</div>
</fieldset>
</form>`
她是我的测试方法
[TestMethod]
public void Login_FinYa_System()
{
IWebElement login = _driver.FindElement(By.XPath("//form[1]"));
IWebElement pass = _driver.FindElement(By.XPath("//form[2]"));
login.Click();
login.SendKeys("helo");
pass.Click();
pass.SendKeys("123");
pass.SendKeys(Keys.Enter);
Assert.AreEqual("hello", "hello");
}
答案 0 :(得分:2)
您在登录名和密码字段中使用了错误的xpath。
试试这个。
WebElement login = driver.findElement(By.xpath("//input[@type='text']"));
WebElement password = driver.findElement(By.xpath("//input[@type='password']"));
login.click();
login.sendKeys("hello");
pass.click();
pass.sendKeys("123");
pass.sendKeys(Keys.Enter);
Assert.assertEquals("hello", "hello");
答案 1 :(得分:1)
正如@Ankur所说,输入不是表单标记的直接子,当你复制登录输入的XPATH时你会得到这个
/ HTML /体/形式/字段集/标签[1] /输入
您必须使用此代码:
WebDriver d = new FirefoxDriver(); // OR YOUR DRIVER
d.get("Your domain");
d.findElement(By.xpath("/html/body/form/fieldset/label[1]/input")).sendKeys("hello");
d.findElement(By.xpath("/html/body/form/fieldset/label[2]/input")).sendKeys("123"); // PASSWORD
d.findElement(By.xpath("/html/body/form/fieldset/label[2]/input")).Click();