C# 打开webbrowser,自动完成表单组件并提交
我们目前正在研究一种创建WPF/winforms应用程序的方法,我们可以在内部设置该应用程序:-C# 打开webbrowser,自动完成表单组件并提交,c#,.net,wpf,winforms,browser,C#,.net,Wpf,Winforms,Browser,我们目前正在研究一种创建WPF/winforms应用程序的方法,我们可以在内部设置该应用程序:- 自动将web浏览器的新实例打开到预定义的URL 使用预定义数据自动完成必填字段 自动提交表单并等待下一页加载 使用预定义数据自动填写必填字段(第2页) 自动提交表单并等待下一页加载(等) 经过大量调查,我们唯一能找到的是通过以下方式打开web浏览器: object o = null; SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplore
object o = null;
SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer();
IWebBrowserApp wb = (IWebBrowserApp)ie;
wb.Visible = true;
wb.Navigate(url, ref o, ref o, ref o, ref o);
对于如何完成该过程,如有任何建议/阅读建议,将不胜感激 我编写了一个在html页面中填充元素的示例。你必须这样做: Winform
public Form1()
{
InitializeComponent();
//navigate to you destination
webBrowser1.Navigate("https://www.certiport.com/portal/SSL/Login.aspx");
}
bool is_sec_page = false;
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (!is_sec_page)
{
//get page element with id
webBrowser1.Document.GetElementById("c_Username").InnerText = "username";
webBrowser1.Document.GetElementById("c_Password").InnerText = "pass";
//login in to account(fire a login button promagatelly)
webBrowser1.Document.GetElementById("c_LoginBtn_c_CommandBtn").InvokeMember("click");
is_sec_page = true;
}
//secound page(if correctly aotanticate
else
{
//intract with sec page elements with theire ids and so on
}
}
Wpf
public MainWindow()
{
InitializeComponent();
webBrowser1.Navigate(new Uri("https://www.certiport.com/portal/SSL/Login.aspx"));
}
bool is_sec_page = false;
mshtml.HTMLDocument htmldoc;
private void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
{
htmldoc = webBrowser1.Document as mshtml.HTMLDocument;
if (!is_sec_page)
{
//get page element with id
htmldoc.getElementById("c_Username").innerText = "username";
//or
//htmldoc.getElementById("c_Username")..SetAttribute("value", "username");
htmldoc.getElementById("c_Password").innerText = "pass";
//login in to account(fire a login button promagatelly)
htmldoc.getElementById("c_LoginBtn_c_CommandBtn").InvokeMember("click");
is_sec_page = true;
}
//secound page(if correctly aotanticate
else
{
//intract with sec page elements with theire ids and so on
}
}
只需导航到特定的URL并填充页面元素。如果我理解正确,您希望在web浏览器中打开一些URL,然后像普通用户一样与站点交互。对于这样的任务,我可以建议看一下。虽然它通常用作回归测试自动化工具,但没有人能阻止您将其用作浏览器自动化工具 硒元素含量丰富,含量大。最有可能的情况是,您希望使用可通过以下途径获得的 下面是典型的Selenium“脚本”的一个小示例(从文档中获取): 就个人而言,我可以建议根据用户操作(注册、登录、填写表单、在网格中选择内容、过滤网格等)来思考和组织脚本。这将为脚本提供良好的形状和可读性,而不是凌乱的硬编码代码块。本例中的脚本可能类似于以下内容:
// Fill username and password
// Click on button "login"
// Wait until page got loaded
LoginAs("johndoe@domain.com", "johndoepasswd");
// Follow link in navigation menu
GotoPage(Pages.Reports);
// Fill inputs to reflect year-to-date filter
// Click on filter button
// Wait until page refreshes
ReportsView.FilterBy(ReportsView.Filters.YTD(2012));
// Output value of Total row from grid
Console.WriteLine(ReportsView.Grid.Total);
谢谢你的快速回复。但是,这在winform上使用内置浏览器控件,而不是打开浏览器的新实例。。这是实现这一目标的唯一方法吗(推荐方法?)@user2009091:您正在使用wpf?我们可以使用/或。。目前,这是一个概念的证明,我们正试图让工作银行你。但是,这仍然依赖于使用浏览器.net控件。。并没有真正打开explorer。。这是唯一的方法吗?@user2009091:trye使用SHDocVw.dll有一篇文章:太棒了!我已经寻找这种类型的解决方案好几天了。很好的答案!非常感谢。
// Fill username and password
// Click on button "login"
// Wait until page got loaded
LoginAs("johndoe@domain.com", "johndoepasswd");
// Follow link in navigation menu
GotoPage(Pages.Reports);
// Fill inputs to reflect year-to-date filter
// Click on filter button
// Wait until page refreshes
ReportsView.FilterBy(ReportsView.Filters.YTD(2012));
// Output value of Total row from grid
Console.WriteLine(ReportsView.Grid.Total);
if (webBrowser1.Document != null)
{
HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement elem in elems)
{
String nameStr = elem.GetAttribute("name");
if (nameStr == "email")
{
webBrowser1.Document.GetElementById(nameStr).SetAttribute("value", "test_email@mail.com");
}
}
}