简体   繁体   English

Windows窗体WebBrowser控件和iframe

[英]Windows Forms WebBrowser control and iframes

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 . 我正在尝试创建一个软件来或多或少地自动化网页上的表单填充 - 我决定在System.Windows.Forms使用WebBrowser控件。 This works great and I can easily manipulate the DOM through webbrowser.Document and so forth. 这很好用,我可以通过webbrowser.Document轻松操作DOM等等。 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. 然而不幸的是,我正在进行自动化的网站有一个文件上传,它在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 InnerDocument attribute on HtmlElement as far as I can see, nor no type that I have found that I can cast HtmlElement to so that I can access the inner DOM. 然后在iframe中提交表单当然 - 但是就我所见, HtmlElement上没有InnerDocument属性,也没有我发现我可以将HtmlElement为可以访问内部DOM的类型。

How this is done? 怎么做的?

Try using the "frames" collection instead. 请尝试使用“frames”集合。 From MSDN : 来自MSDN

The iframe element functions as a document within a document, or like a floating frame. iframe元素用作文档中的文档,或者像浮动框架。 The frames collection provides access to the contents of an iframe. frames集合提供对iframe内容的访问。 Use the frames collection to read or write to elements contained in an iframe. 使用frames集合读取或写入iframe中包含的元素。 For example, the syntax for accessing the backgroundColor style of the body object in an iframe is: 例如,访问iframe中body对象的backgroundColor样式的语法是:

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

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

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