2014-02-26 112 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是正确的。另一个问题可能是密码字段在某种程度上来自于自动化程序,或者字段实际上不接受字符串输入。