Windows窗体WebBrowser控件和iframe中窗体、控件、Windows、iframe

2023-09-03 16:58:35 作者:我蓄长发,只待君归。

我想创建一个软件或多或少自动表单填充网页上的 - 我已经决定使用 System.Windows.Forms的WebBrowser控件。这个伟大的工程,我可以很容易地操纵 DOM 中通过 webbrowser.Document 等等。然而不幸的是,我做的自动化在网站上有一个文件的上传是跑在iframe内 - 这是我在哪里卡住了,我根本无法工作,如何能够操纵iframe的DOM中的元素

理想的情况是我想要做的是一样的东西:

 的HtmlElement IFRAME = browser.Document.GetElementById(iframe_id);
iframe.InnerDocument.GetElementById(file_upload_input)的setAttribute(价值,myfile.txt的)。
 

,然后提交表单过程中的iframe中 - 但是没有任何关于 InnerDocument 属性的HtmlElement 远我所看到的,也不是没有类型,我发现,我可以投的HtmlElement 来,这样我可以访问内部DOM。

如何做到这一点?

解决方案

尝试使用帧集合,而不是。从 MSDN :

  

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

     

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

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 DOM through webbrowser.Document and 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.

C 中WebBrowser控件内打开的网页含有Iframe,对IFrame中网页中控件的赋值该如何操作呢

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.

How this is done?

解决方案

Try using the "frames" collection instead. From 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;