我尝试使用c#中的Web浏览器登录网站("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
}
}
登录成功后,网址为"https://www.mytel.com.mm/myviettel"。
我认为 Logurl 变量应该包含("https://www.mytel.com.mm/myviettel"(。
谢谢
为了答案,我使用Selenium Chromedriver服务,该服务可以通过编程方式成功登录。