Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding and clicking a button with no ID within html code in WebBrowser

Tags:

browser

c#

I basically have a web-browser control that is going through and automatically completing some forms, it's been a breeze so far, however I've gotten to a form that does not have the "submit " or "reset" button labelled with ID or Name. (I need to be able to click both) Example:

Submit Button

<td align="right" valign="middle" class="LeftSide" style="padding-right: 20; padding-top: 10;"><button class="Search" type="submit" tabindex="7"><b>Search</b></button></td>

Reset Button

<td align="left" valign="middle" class="RightSide" style="padding-left: 20; padding-top: 10;" colspan="2"><button class="Search" type="reset" tabindex="8"><b>Clear</b></button></td>

How would i search the HtmlDocument for these button's and click them? They're nested as the following:

<body><table><form><tr><td><button>'s
like image 233
caesay Avatar asked May 04 '12 04:05

caesay


2 Answers

something like that, maybe ?

public void ClickButton(string type) {
    var button = myWebBrowser.Document.GetElementsByTagName("button")
             .Cast<HtmlElement>()
             .FirstOrDefault(m => m.GetAttribute("type") == type);
    if (button != null)
        button.InvokeMember("click"); 
}

Usage

ClickButton("reset");
ClickButton("submit");
like image 75
Raphaël Althaus Avatar answered Oct 07 '22 19:10

Raphaël Althaus


<td align="right" valign="middle" class="LeftSide" style="padding-right: 20; padding-top: 10;"><input type="submit" value="Submit" /></td>

<td align="left" valign="middle" class="RightSide" style="padding-left: 20; padding-top: 10;" colspan="2"><input type="reset" value="Reset" /></td>


HtmlElementCollection elc = this.webBrowser1.Document.GetElementsByTagName("input");  
foreach (HtmlElement el in elc)  
{  
   if (el.GetAttribute("type").Equals("submit"))  
   {  
        el.InvokeMember("click");  
   }  
 }
like image 30
Ponmalar Avatar answered Oct 07 '22 21:10

Ponmalar