Windows 窗体 WebBrowser 控件和 iframe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1435172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:08:47  来源:igfitidea点击:

Windows Forms WebBrowser control and iframes

c#.netwindowsiframebrowser

提问by kastermester

I am trying to create a piece of software to more or less automate form-fillings on a webpage - and I have decided to use the WebBrowser control in System.Windows.Forms. This works great and I can easily manipulate the DOMthrough webbrowser.Documentand so forth. However unfortunately the site that I am doing automation on has a file upload which is ran inside an iframe - and this is where I am stuck, I simply cannot work out how to be able to manipulate elements inside the DOM of the iframe.

我正在尝试创建一个软件来或多或少地自动化网页上的表单填写 - 我决定在System.Windows.Forms. 这很好用,我可以轻松地通过等等操作DOMwebbrowser.Document。然而不幸的是,我正在执行自动化的站点有一个在 iframe 内运行的文件上传 - 这就是我被卡住的地方,我根本无法弄清楚如何能够操作 iframe 的 DOM 内的元素。

Ideally what I'd like to do is something like:

理想情况下,我想做的是:

HtmlElement iframe = browser.Document.GetElementById("iframe_id");
iframe.InnerDocument.GetElementById("file_upload_input").SetAttribute("value", "myfile.txt");

And then submit the form inside the iframe of course - however there is no InnerDocumentattribute on HtmlElementas far as I can see, nor no type that I have found that I can cast HtmlElementto so that I can access the inner DOM.

然后当然在 iframe 内提交表单 - 但是就我所见,没有任何InnerDocument属性HtmlElement,也没有我发现可以转换的类型,HtmlElement以便我可以访问内部 DOM。

How this is done?

这是怎么做的?

回答by David

Try using the "frames" collection instead. From MSDN:

尝试改用“frames”集合。从MSDN

The iframe element functions as a document within a document, or like a floating frame. The frames collection provides access to the contents of an iframe. Use the frames collection to read or write to elements contained in an iframe.For example, the syntax for accessing the backgroundColor style of the body object in an iframe is:

sColor = document.frames("sFrameName").document.body.style.backgroundColor;

iframe 元素用作文档中的文档,或类似于浮动框架。框架集合提供对 iframe 内容的访问。使用框架集合读取或写入 iframe 中包含的元素。例如,在 iframe 中访问 body 对象的 backgroundColor 样式的语法是:

sColor = document.frames("sFrameName").document.body.style.backgroundColor;