Click a button in an iframe in a WebBrowser control

I use C #, win applications by trying to click the button found in the iframe on my web browser.

HTML:

<iframe frameborder="0" scrolling="no" id="EditorIFrmae" name="EditorIFrmae" width="100%" height="700" style="z-index: 0; height: 852px;" src="X"></iframe> 

In iframe code:

 <div height="" width="100%" style="position: relative; min-height: 50px;" onclick="javascript:funcEditPage('SB_Content_Page','SB_Content_PagePreview','PAGE5.asp');" class="SB_DivContentPreview" id="SB_Content_PagePreview"> </div> 

My code is:

 HtmlElementCollection linkit = this.webBrowser1.Document.GetElementsByTagName("div"); foreach (HtmlElement foundit in linkit) { if (foundit.GetAttribute("id").Equals("SB_Content_PagePreview")) { foundit.InvokeMember("Click"); } } 

How can I press a button?

+5
source share
3 answers

You can get the frame using Document.Window.Frames[] , which accepts the identifier (string) or index (int) of the frame.

You should also consider that you should wait until the DocumentCompleted event, and then do your job. If there is some iframe on the page, the DocumentCompleted event fires more than once and, using the code below, checking the eventarg URL, we are sure that this is the DocumentCompleted event of the main page that we need, then you can enable the flag to confirm that the document completed, and here or somewhere else, find the frame and the desired item.

Where is the code:

 public partial class Form1 : Form { private void Form1_Load(object sender, EventArgs e) { this.webBrowser1.Navigate(@"d:\test.html"); this.webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted; } bool completed = false; void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { if (e.Url == webBrowser1.Document.Url) { completed = true; } } private void clickToolStripButton_Click(object sender, EventArgs e) { if(completed) { var frame = webBrowser1.Document.Window.Frames["iframeid"]; var button = frame.Document.GetElementById("buttonid"); button.InvokeMember("click"); } } } 

The contents of test.html:

 <html> <head> <title>OUTER</title> </head> <body> Some Content <br/> Some Content <br/> Some Content <br/> iframe: <iframe src="test2.html" id="iframeid"></iframe> </body> </html> 

The contents of test2.html:

 <html> <head> <title>IFRAME</title> </head> <body> <button id="buttonid" onclick="alert('Clicked')">Click Me</button> </body> </html> 

Screenshot:

enter image description here

+3
source

Before you try to find elements in it, you should wait until the document is fully loaded , and when loading a frame, you should search inside the frame .
Subscribe to the DocumentCompleted event :

 initialUrl = new Uri(/* your url */); webBrowser.DocumentCompleted += OnDocumentCompleted; webBrowser.Navigate(initialUrl); 

and then try to find the elements inside the method:

  private void OnDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs args) { if (args.Url == initialUrl) { HtmlElementCollection elements = webBrowser.Document.Window.Frames[0].Document.GetElementsByTagName("div"); foreach (HtmlElement element in elements) { if (element.GetAttribute("id").Equals("SB_Content_PagePreview")) { element.InvokeMember("Click"); } } } } 
0
source
  HtmlElementCollection linkit = this.webBrowser1.Document.GetElementsByTagName("iframe"); foreach (HtmlElement foundit in linkit) { if (foundit.GetAttribute("id").Equals("EditorIFrmae")) { string iframeurl = foundit.GetAttribute("src"); } } 

This is the option that I use to get the iframe link, and then go to that link.

0
source

Source: https://habr.com/ru/post/1232897/


All Articles