Facebook用Selenium登录C#

我一直在玩Selenium C#框架并试图登录facebook,但没有运气。

这就是我到目前为止(基于这篇文章: 使用Selenium测试Facebook Connect应用程序? )。 我不能让它工作。

ISelenium sel = new DefaultSelenium("localhost", 4444, "*chrome", "http://facebook.com"); public void LoginWithEmailAndPassword(string email, string pass) { sel.Start(); sel.Open("http://www.facebook.com"); sel.ClickAt("//img[\\@alt='Facebook']", "User clicks on Facebook Login"); sel.WaitForPopUp("", "3000"); sel.SelectPopUp(""); sel.Type("email", email); sel.Type("pass", pass); sel.KeyPress("pass", "\\13"); sel.SelectWindow("null"); } 

因此,如果有人可以为我提供一些如何执行此操作的示例代码,或指导我朝着正确的方向发展,那么我们将不胜感激。

解决

通过使用Firefox Selenium插件的记录function,我获得了id和我需要的function才能使它工作。 登录按钮似乎需要XPath定义,我也从Selenium IDE获得。

 ISelenium sel = new DefaultSelenium("localhost", 4444, "*chrome", "http://facebook.com"); public void LoginWithEmailAndPassword(string email, string pass) { sel.Start(); sel.Open("/"); sel.Type("id=email", email); sel.Type("id=pass", pass); sel.Click("//label[@id='loginbutton']/input"); } 

尝试使用WebDriver:

 IWebDriver driver = new FireFoxDriver(); driver.GoToUrl("www.facebook.com"); var element = driver.FindElement(By.Id("email")); element.SendKeys(email); ... element = driver.FindElement(By.Id("submit button id")); element.Click(); 

此链接可以帮助您开始。