无法使用 c# 中的 webbrowser 以编程方式登录网站
Cannot Login into a website programmatically using webbrowser in c#
我尝试在 c# 中使用 webbrowser 登录网站 ("https://mytel.com.mm/")。
无法登录,下面是代码。
/// <summary>
/// The main entry point for the application.
/// </summary>
private static WebBrowser wb;
[STAThread]
static void Main()
{
wb = new WebBrowser();
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
wb.Navigate("https://mytel.com.mm/");
Application.Run();
}
static void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string Logurl = wb.Url.AbsoluteUri;
if (!wb.Url.ToString().Contains("myviettel"))
{
var InputElements = wb.Document.GetElementsByTagName("input");
foreach (HtmlElement i in InputElements)
{
if (i.GetAttribute("ng-model").Equals("loginForm.username"))
{
i.Focus();
i.InnerText = "xxxxxxxxx";
}
if (i.GetAttribute("ng-model").Equals("loginForm.password"))
{
i.Focus();
i.InnerText = "xxxxxxxxx";
i.RemoveFocus();
}
}
var buttonElements = wb.Document.GetElementsByTagName("button");
foreach (HtmlElement b in buttonElements)
{
if (b.GetAttribute("className").Equals("btn btn-nat-input-popup-bottom margin-bottom-10 ng-binding"))
{
b.InvokeMember("click");
}
}
}
else
{
//logged in
}
}
登录成功后,url为“https://www.mytel.com.mm/myviettel”。
而且我认为 Logurl 变量应该包含 ("https://www.mytel.com.mm/myviettel").
谢谢
为了回答问题,我使用了可以通过编程方式成功登录的 Selenium Chromedriver 服务。
我尝试在 c# 中使用 webbrowser 登录网站 ("https://mytel.com.mm/")。 无法登录,下面是代码。
/// <summary>
/// The main entry point for the application.
/// </summary>
private static WebBrowser wb;
[STAThread]
static void Main()
{
wb = new WebBrowser();
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
wb.Navigate("https://mytel.com.mm/");
Application.Run();
}
static void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string Logurl = wb.Url.AbsoluteUri;
if (!wb.Url.ToString().Contains("myviettel"))
{
var InputElements = wb.Document.GetElementsByTagName("input");
foreach (HtmlElement i in InputElements)
{
if (i.GetAttribute("ng-model").Equals("loginForm.username"))
{
i.Focus();
i.InnerText = "xxxxxxxxx";
}
if (i.GetAttribute("ng-model").Equals("loginForm.password"))
{
i.Focus();
i.InnerText = "xxxxxxxxx";
i.RemoveFocus();
}
}
var buttonElements = wb.Document.GetElementsByTagName("button");
foreach (HtmlElement b in buttonElements)
{
if (b.GetAttribute("className").Equals("btn btn-nat-input-popup-bottom margin-bottom-10 ng-binding"))
{
b.InvokeMember("click");
}
}
}
else
{
//logged in
}
}
登录成功后,url为“https://www.mytel.com.mm/myviettel”。
而且我认为 Logurl 变量应该包含 ("https://www.mytel.com.mm/myviettel").
谢谢
为了回答问题,我使用了可以通过编程方式成功登录的 Selenium Chromedriver 服务。