简体   繁体   English

C#中的简单浏览器控件

[英]Simple Browser Control in C#

I have a simple C# WinForms Crud AP for employees that have to do a lot of simple web lookups. 对于需要进行大量简单Web查找的员工,我有一个简单的C#WinForms Crud AP。 Currently it loads 5 websites and puts a search term into the clipboard. 目前它加载5个网站并将搜索词放入剪贴板。 The employee then has to paste the search term into the search textbox on each of the sites and hit enter. 然后,员工必须将搜索词粘贴到每个站点的搜索文本框中,然后按Enter键。 Is there a simple way to enter the data for them? 有没有简单的方法为他们输入数据? The search textbox usually has focus when the sites load, I just cannot find a way to detect when the page has loaded or how to send the data to the window. 搜索文本框通常在网站加载时具有焦点,我只是找不到检测页面何时加载或如何将数据发送到窗口的方法。

It looks, from these helpful answers, like I was going about this the wrong way. 从这些有用的答案来看,我看起来就是错误的方式。 I was using code found with the help of my good friend, Mr. Google 我在我的好朋友谷歌先生的帮助下使用了代码

System.Diagnostics.Process.Start("www.website.com");
Clipboard.SetText(textBox1.Text, TextDataFormat.Text);

I will do some more research this week on the webbrowser control 我将在本周的webbrowser控件上做更多的研究

You can use WebBrowser's DocumentCompleted event to be notified when the page has finished loading. 您可以在页面加载完成后通知WebBrowser的DocumentCompleted事件。

If the textbox you're entering your text into has a unique id, you can use WebBrowser.Document.GetElementById: 如果您输入文本的文本框具有唯一ID,则可以使用WebBrowser.Document.GetElementById:

webBrowser.Document.GetElementById("some_id").InnerText = theSearchText;

GetElementById returns an instance of HtmlElement (or null, if the element wasn't found). GetElementById返回HtmlElement的实例(如果未找到该元素,则返回null)。 Note that ".InnerText" might not work for all element types, there's also InnerHtml and lots of other properties, I'd recommend playing around with the HtmlElement class before deciding how to use it. 请注意“.InnerText”可能不适用于所有元素类型,还有InnerHtml和许多其他属性,我建议在决定如何使用它之前使用HtmlElement类。

Let me know if you don't have a unique id for the element, there's lots of otherways to find an element. 如果您没有元素的唯一ID,请告诉我,还有很多其他方法可以找到元素。 including using the exposed DOM via Document.Body. 包括通过Document.Body使用暴露的DOM。

You should be able to hook into the Webbrowser's Navigated event that should tell you when the page is Loaded (I THINK! I'm not 100% sure). 您应该可以挂钩Webbrowser的Navigated事件,该事件应该告诉您页面何时加载(我认为!我不是100%肯定)。 As for setting the text, if you're sure that the textbox has focus you can do something like this: 至于设置文本,如果你确定文本框有焦点,你可以这样做:

        Clipboard.SetText("Stackoverflow");
        this.webBrowser1.Document.ExecCommand("Paste", false, null);

The WebBrowser's DocumentCompleted event is fired when a page (or a frame!) is loaded. 加载页面(或框架!)时会触发WebBrowser的DocumentCompleted事件。 When a page has frames, the DocumentCompleted event fires multiple times. 当页面具有框架时,DocumentCompleted事件会多次触发。 In the DocumentCompleted event you compare the ReadyState property with ReadyState.Completed to check if it is fully loaded. 在DocumentCompleted事件中,您将ReadyState属性与ReadyState.Completed进行比较,以检查它是否已完全加载。

But in general, using SendKeys is not the best way to do this. 但一般来说,使用SendKeys并不是最好的方法。 Matthew Brindley's answer (to use GetElementById) is the best way, although this does not work for File input controls. Matthew Brindley的答案(使用GetElementById)是最好的方法,虽然这对文件输入控件不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM