2012-12-27 92 views
1

我需要使用selenium webdriver訪問文本框和按鈕id。由於它放置在contentplaceholder內,因此webdriver無法找到控件的標識。好心地建議我如何找到位於contentplaceholder內的控件。我想我需要爲此給路徑。你的想法會更有幫助。使用selenium webdriver訪問contentplaceholder屬性

這是我的aspx代碼:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> 
<table> 
<tr> 
    <td> <asp:Label ID="lblName" runat="server" Text="Name :" ></asp:Label> </td> 
    <td> <asp:TextBox ID="txtName" runat="server" 
      ToolTip="Enter your registered user name"></asp:TextBox> </td> 

</tr> 
<tr> 
    <td> <asp:Label ID="lblPwd" runat="server" Text="Password :" ></asp:Label> </td> 
    <td> <asp:TextBox ID="txtPwd" runat="server" 
      ToolTip="Enter your registered password"></asp:TextBox> </td> 

</tr> 
<tr> 
    <td> 
     <asp:Button ID="btnLogin" runat="server" Text="Login" onclick="btnLogin_Click" 
      ToolTip="Click to Login"/> </td> 
    <td> <asp:Label ID="lblWarning" runat="server" Text=" "></asp:Label> </td> 
</tr> 
</table> 
</asp:Content> 

我testproject代碼:

[TestMethod()] 
    public void TestMethod1() 
    { 
     //try 
     //{ 
     driver.Navigate().GoToUrl("http://localhost:50294/Login.aspx"); 
     driver.FindElement(By.Id("txtname")).SendKeys("sachin");    
     driver.FindElement(By.Id("btnLogin")).Click(); 
     Label lbl = page.FindControl("lblWarning") as Label; 
     Assert.AreEqual(lbl, "Inserted successfully"); 
     } 
     catch (Exception er) 
     { 
      Assert.Fail(er.Message); 

     } 

    } 

在此先感謝。

回答

2

感謝您的答覆。但這不適用於我的情況。

但是我能找到解決我的問題。

我通過其在頁源選項,瀏覽器動態生成的id替換文本框和按鈕控制的ID。現在Web驅動程序可以找到這些控件並將值插入到文本框中。

這是我的新代碼:

driver.FindElement(By.Id("MainContent_txtName")).SendKeys("sachin"); 
driver.FindElement(By.Id("MainContent_btnLogin")).Click(); 

上面的代碼工作正常:)

1

由於您的文本框和按鈕位於內容佔位符內,因此您可以嘗試通過內容佔位符訪問這些元素。

例如: driver.FindElement(By.Id( 「的BodyContent」))(By.Id( 「txtName的」))的SendKeys( 「薩欽」);。。

希望這有助於和對你的作品:)

相關問題