问题描述
我实际上正在为特定网站开发一个登录项目,而不是去网站输入登录凭据(用户名和密码)我的客户从我的Windows窗体应用程序C#中输入这些凭据要容易得多。我使用WebBrowser控件使用Windows窗体应用程序的内置功能完成了此操作。但是,我想进一步在客户的默认Web浏览器中执行此操作,而不是利用WinForms Application中的Web浏览器控件。 DropBox在他们的Windows和Mac应用程序中完成了这项工作。我该怎么做?
启用了WebBrowser控件功能的C#中实现的以下代码:
从网站上获取的用户名框:_ ctl0_laxMolHolderMain_Loginapp2_txtLoginUsername
从网站上取下的密码框:_ ct0_010_HolderMain_Loginapp2_txtLoginPassword
从网站上获取的登录按钮:_ ct0.0laceHolderMain_Loginapp2_btnLoginLogin
I am actually working on a login project for a specific website, instead of going to the website to enter the login credentials (Username an Password) It would be much easier for my customer to enter those credentials from my Windows Forms Application C#. I have accomplished this using the built in feature of Windows Forms Application using the WebBrowser Control. However, I want to go further and do this in a customer's default web browser rather than taking advantage of the Web Browser Control that is inside WinForms Application. DropBox has done this in their Windows and Mac applications. How can I do this?
The following code implemented in C# with WebBrowser Control feature enabled:
Username box taken from the site: "_ctl0_PlaceHolderMain_Loginapp2_txtLoginUsername"
Password box taken from the site: "_ctl0_PlaceHolderMain_Loginapp2_txtLoginPassword"
Login Button taken from the site: "_ctl0_PlaceHolderMain_Loginapp2_btnLoginLogin"
using System.Windows.Forms.HtmlElement;
HtmlElementCollection theElementCollection;
theElementCollection = Browser.webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement curElement in theElementCollection)
{
if ((curElement.GetAttribute("id").ToString() == "_ctl0_PlaceHolderMain_Loginapp2_txtLoginUsername"))
{
curElement.SetAttribute("Value", id.Text);
}
else if ((curElement.GetAttribute("id").ToString() == "_ctl0_PlaceHolderMain_Loginapp2_txtLoginPassword"))
{
curElement.SetAttribute("Value", pw.Text);
}
}
theElementCollection = Browser.webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement curElement in theElementCollection)
{
if (curElement.GetAttribute("id").Equals("_ctl0_PlaceHolderMain_Loginapp2_btnLoginLogin"))
{
curElement.InvokeMember("click");
}
}
推荐答案
这篇关于如何在安装的默认Web浏览器中传递字符串(用户名和密码),而不是使用Web浏览器控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!