2014-02-26 107 views
0

我一直在C#中使用Windows窗體中的selenium在頁面上輸入用戶名和密碼,然後單擊登錄,然後執行一些更多操作。這工作正常,但後面的步驟將涉及測量Firefox對話框出現所需的時間,我認爲這不能用硒完成。因此,我正試圖遷移到webdriver。當我嘗試使用webdriver.sendkeys輸入用戶名和密碼時,它會將光標置於正確的框中,但不輸入任何文本。我的硒代碼是:我使用Selenium Webdriver sendkeys()不工作

namespace Project1 
{ 
    public partial class Form1 : Form 
    { 
      public Form1() 
      { 
       Process.Start("D:\\startSelenium.bat"); 
       InitializeComponent(); 
      } 

      private void button1_Click(object sender, EventArgs e) 
      { 
       ISelenium selenium = new DefaultSelenium("localhost", 4444, "*firefox", <baseURL>); 
       selenium.Start(); 
       selenium.Open(<loginpage>); 
       selenium.Type("id=UserName", <username>); 
       selenium.Type("id=Password", <password>); 
       selenium.Click("css=input.button"); 
       selenium.WaitForPageToLoad("40000"); 
       ... 

的webdriver的代碼是:

namespace Project2 
{ 
    public partial class Form1 : Form 
    { 
      public Form1() 
      { 
       Process.Start("D:\\startSelenium2.bat"); 
       InitializeComponent(); 
      } 

      private void button1_Click(object sender, EventArgs e) 
      { 
       FirefoxProfile profile = new FirefoxProfile(C:\\Selenium"); 
       IWebDriver driver = new FirefoxDriver(profile); 
       driver.Navigate().GoToUrl(<loginpage>); 
       driver.FindElement(By.Id("UserName")).Clear(); 
       driver.FindElement(By.Id("UserName")).SendKeys(<username>); 
       driver.FindElement(By.Id("Password")).Clear(); 
       driver.FindElement(By.Id("Password")).SendKeys(<password>); 
       ... 

它卡住在行

driver.FindElement(By.Id("UserName")).SendKeys(<username>); 

我在做什麼錯?

+0

我假設爲您實際上是在輸入用戶名和密碼字符串?此外,什麼類型的HTML元素是「用戶名」和「密碼」?我只問,因爲你的代碼看起來是正確的,所以我只是想知道是否有任何細節遺漏。 – mmilleruva

+0

什麼版本的Selenium + Firefox? – Arran

+0

Selenium 2.31.0和firefox 3.6.12(儘管希望它可以使用多個版本)。 是的,我使用真正的字符串作爲用戶名和密碼,並且元素是 user3355764

回答

0

它給了什麼錯誤?如果它說,元素不存在,這意味着你的

FindElement(By.Id("Password")) 

不工作,你需要仔細檢查您試圖選擇元素的ID是正確的。另一個問題可能是密碼字段在某種程度上來自於自動化程序,或者字段實際上不接受字符串輸入。