2015-10-04 25 views
5

Sto utilizzando C#, vinci app, cercando di fare clic su un pulsante trovato in un iframe sul mio browser.Fare clic su Button in iframe nel controllo WebBrowser

HTML:

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

Nel codice iframe:

<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> 

Il mio codice:

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

Come sono capace di fare clic sul pulsante?

+0

domanda molto chiara. Qual è il problema essere più specifici –

+0

@FarhanAnam - Sono sry non era abbastanza chiaro: Sto cercando di fare clic su un div che si trova in ifame e non sulla pagina curret. – Sagi

+0

Il tuo codice sembra bene qual è il problema che stai affrontando? –

risposta

0

è necessario attendere documento da completamente caricato prima di cercare di trovare gli elementi in esso e come si carica una cornice, è necessario cercare all'interno di una cornice troppo.
Iscriviti a DocumentCompleted evento:

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

e poi cercare di trovare elementi all'interno del metodo:

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"); 
       } 
      } 
     } 
    } 
+1

Grazie per aver cercato di aiutarmi, ma non aiuta molto. – Sagi

+0

Sì. Mi è mancato l'uso del frame. – Fab

0
HtmlElementCollection linkit = this.webBrowser1.Document.GetElementsByTagName("iframe"); 
      foreach (HtmlElement foundit in linkit) 
      { 
       if (foundit.GetAttribute("id").Equals("EditorIFrmae")) 
       { 
        string iframeurl = foundit.GetAttribute("src"); 
       } 
      } 

Thats un'opzione che io uso per ottenere il link iframe e quindi passare a questo link.

3

È possibile ottenere un frame utilizzando Document.Window.Frames[] che accetta id (stringa) o indice (int) di frame.

Inoltre, dovresti considerare di attendere fino all'innesco dell'evento DocumentCompleted e poi fare il tuo lavoro. Quando ci sono alcuni iframe in una pagina, l'evento DocumentCompleted si attiva più di una volta e usando il codice sottostante controllando l'url di eventarg ci assicuriamo che questo sia l'evento DocumentCompleted della pagina principale di cui abbiamo bisogno, quindi puoi abilitare una bandiera per dire la il documento è completato e qui o altrove, trova il frame e l'elemento che desideri.

Dove si trova il codice:

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"); 
     } 
    } 
} 

Contenuto 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> 

Contenuto 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

+0

Grazie !!!!!! – Sagi

+0

@Sagi Siete i benvenuti :) –