0

我想從selenium的以下表單中選擇輸入字段。我嘗試了很多方法,但沒有找到任何幫助,我的測試總是失敗。 這裏是HTML表單:從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"); 
} 

回答

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

它不工作我試過了。這裏是另一種方法,我已經嘗試過_driver.FindElement(By.XPath(「// form [@ class ='login']/input [1]」))'它不工作 – Amjad

+0

輸入不是直接子對象表單標籤。試試這個 - // form [@ class ='login'] // input [1] – Ankur

+0

,這也是行不通的。 – Amjad

1

正如@Ankur說,輸入不是形式標記的直接孩子,當你複製登錄輸入的XPATH你得到這個

/html/body/form/fieldset/label[1]/input

你有此代碼工作:

 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(); 
+0

謝謝!這工作。 – Amjad

+0

@Amjad你'歡迎光臨! :))我向你推薦一本C#與硒編輯的Apress –

+0

感謝您的建議,我有這本書的副本。 – Amjad

相關問題