简体   繁体   English

Facebook用Selenium登录C#

[英]Facebook Login with Selenium for C#

I've been playing with the Selenium C# framework and been trying to do a facebook login, but without any luck. 我一直在玩Selenium C#框架并且一直在尝试登录facebook,但没有运气。

This is what i got so far (based on this post: Testing a Facebook Connect application using Selenium? ). 这是我到目前为止所得到的(基于这篇文章: 使用Selenium测试Facebook Connect应用程序? )。 I can't get it to work though. 我不能让它工作。

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");
}

So if someone could provide me with some sample code for how to do this, or guide me in the right direction, it would be appreciated. 因此,如果有人可以为我提供一些如何执行此操作的示例代码,或者指导我朝正确的方向发展,那么我们将不胜感激。

Resolved 解决

By using the record feature of the Firefox Selenium plugin I got the id's and the functions i needed to get it working. 通过使用Firefox Selenium插件的记录功能,我获得了id和我需要的功能才能使它工作。 The login button seemed to require an XPath definition, which I also got from the Selenium IDE. 登录按钮似乎需要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");
}

Try use WebDriver: 尝试使用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();

This link could help you get start. 链接可以帮助您开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM