带Web浏览器控件的C#windows应用程序 [英] C# windows application with web browser control

查看:80
本文介绍了带Web浏览器控件的C#windows应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ovide CodeHi全部,

我正在开发一个C#windows应用程序,我正在使用Windows Web浏览器控件来自动化Web应用程序。我正在使用webBrowser1_DocumentCompleted事件下载它正在运行的文件,但最后它弹出窗口消息框,因为我正在使用带有sendKey的计时器事件,所以下载正在发生但之后它会转到其他页面而不是主页。



我该怎么办请咨询。



以下我提供代码



private void webBrowser1_DocumentCompleted(object sender,WebBrowserDocumentCompletedEventArgs e)

{

if((bFirstTime == true)&&(e.Url.ToString ().Contains(sPageLogin)))//仅第一次

{

toolStripTextBox1.Text = webBrowser1.Url.ToString();

bFirstTime = false;

HtmlElement heElement;

HtmlDocument doc = webBrowser1.Document;

HtmlElement userName = doc.GetElementById(userName) ;

HtmlElement密码= doc.GetEle mentById(password);

if(userName!= null)

userName.SetAttribute(value,hello);

if(密码!= null)

password.SetAttribute(value,thnaks);

System.Threading.Thread.Sleep(2000);



foreach(webBrowser1.Document.GetElementsByTagName中的HtmlElement el(input) )

{

if(el.Name ==login&& el.OuterHtml.Contains(javascript:doSubmit())) >
{

el.InvokeMember(点击);

System.Threading.Thread.Sleep(2000);

}

}



}

其他

{

if((e.Url.ToString()。包含(/ 1 / roleDefine.do))||(e.Url.ToString()。包含(/ abc / xyz / mno / 1 / retrieveHomePage .do)))

{//https://applications.abc.com/xyz/mno/mtnl/1/roleDefine.do

foreach(HtmlElement el在webBrowser1.Document中。 GetElementsByTagName(a))

{

if(el.OuterHtml.Contains(/ tdct / prod / wlbx / 1 / D624D669 / en / images / buttons / home_images.jpg))

{

el.InvokeMember(点击);

}

}



}

}

else if((e.Url.ToString()。包含(/ tdct / prod / wlbx / 1 / transaction / searchCheck.do)))

{

//httpshttps://applications.abc.com/xyz/mno/ mtnl / 1 / searchCheck.do

foreach(webBrowser1.Document.GetElementsByTagName(a)中的HtmlElement el)

{

if( el.OuterHtml.Contains(/ tdct / prod / wlbx / common / images / icons / save.gif))

{

el.Invo keMember(点击);

}

}

isHasAttachment = true;

if(isHasAttachment)

{

SendKeys.Send({Enter});





//System.Threading.Thread.Sleep(5000);



System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();

t.Interval = 15000;

t.Tick + = new EventHandler(timer_Tick);

t.Start();

}

} $ / $


void timer_Tick(对象发送者,EventArgs e)

{

SendKeys.Send({TAB };;

SendKeys.Send({TAB});

SendKeys.Send({TAB});

SendKeys.Send({ENTER});



}

}

感谢All



我尝试了什么:



C#windows应用程序Web浏览器控件的问题自动化Web应用程序

解决方案

每当您想要更改页面时,请使用

 webBrowser1.Navigate( URL); 


ovide CodeHi All,
I am developing a C# windows application where I am using windows web browser control to automated a web application. I am using webBrowser1_DocumentCompleted event to download file it is working but at the end it popup windows message box for that I am using timer event with sendKey so download is happening but after that it goes to other pages not in home page.

What should I do Please advice.

Below I provide Code

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if ((bFirstTime == true) && (e.Url.ToString().Contains(sPageLogin))) //only first time
{
toolStripTextBox1.Text = webBrowser1.Url.ToString();
bFirstTime = false;
HtmlElement heElement;
HtmlDocument doc = webBrowser1.Document;
HtmlElement userName = doc.GetElementById("userName");
HtmlElement password = doc.GetElementById("password");
if (userName != null)
userName.SetAttribute("value", "hello");
if (password != null)
password.SetAttribute("value", "thnaks");
System.Threading.Thread.Sleep(2000);

foreach (HtmlElement el in webBrowser1.Document.GetElementsByTagName("input"))
{
if (el.Name == "login" && el.OuterHtml.Contains("javascript:doSubmit()"))
{
el.InvokeMember("Click");
System.Threading.Thread.Sleep(2000);
}
}

}
else
{
if ((e.Url.ToString().Contains("/1/roleDefine.do")) || (e.Url.ToString().Contains("/abc/xyz/mno/1/retrieveHomePage.do")))
{ //https://applications.abc.com/xyz/mno/mtnl/1/roleDefine.do
foreach (HtmlElement el in webBrowser1.Document.GetElementsByTagName("a"))
{
if (el.OuterHtml.Contains("/tdct/prod/wlbx/1/D624D669/en/images/buttons/home_images.jpg"))
{
el.InvokeMember("Click");
}
}

}
}
else if ((e.Url.ToString().Contains("/tdct/prod/wlbx/1/transaction/searchCheck.do")))
{
//httpshttps://applications.abc.com/xyz/mno/mtnl/1/searchCheck.do
foreach (HtmlElement el in webBrowser1.Document.GetElementsByTagName("a"))
{
if (el.OuterHtml.Contains("/tdct/prod/wlbx/common/images/icons/save.gif"))
{
el.InvokeMember("Click");
}
}
isHasAttachment = true;
if (isHasAttachment)
{
SendKeys.Send("{Enter}");


//System.Threading.Thread.Sleep(5000);

System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
t.Interval = 15000;
t.Tick += new EventHandler(timer_Tick);
t.Start();
}
}

void timer_Tick(object sender, EventArgs e)
{
SendKeys.Send("{TAB}");
SendKeys.Send("{TAB}");
SendKeys.Send("{TAB}");
SendKeys.Send("{ENTER}");

}
}
Thanks to All

What I have tried:

problem with C# windows application web browser control to automated a web application

解决方案

whenever you want to change the page, use

webBrowser1.Navigate ("URL");


这篇关于带Web浏览器控件的C#windows应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆