How can I click a checkbox using HtmlAgilityPack

c# html-agility-pack

Question

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(webBrowser.DocumentText);
HtmlNode n = doc.DocumentNode.SelectSingleNode("//*[contains(@class, 'checkbox')]");

<table class="list" cellspacing="0">
<tbody>
  <tr>
    <th width="45" class="checkAll"><input title="Select all" class="checkbox" type="checkbox" value=""></th>

    <th><span>Account</span></th>

    <th><span>Target system</span></th>
  </tr>

  <tr class="checkable">
    <td><input name="item1" class="checkbox" type="checkbox"></td>

    <td><span>sometext1</span></td>

    <td><span>system1</span></td>
  </tr>

  <tr class="checkable odd">
    <td><input name="item2" class="checkbox" type="checkbox"></td>

    <td><span>sometext2</span></td>

    <td><span>stystem2</span></td>
  </tr>

  <tr class="foot">
    <td colspan="4"><input name="SUBMIT" class="submit" type="submit" value="Unlock"></td>
  </tr>
  </tbody>
  </table>

I'm attempting to pick every checkbox below by clicking the first one that reads "Select all."

Although I believe my C# code is supposed to load the HTMLNode, I am unable to do it.

Any advice would be appreciated.

1
0
5/14/2015 4:59:11 PM

Accepted Answer

HtmlAgilityPack probably can't be used to check a checkbox. Undoubtedly, you may alter the dom in yourdoc a checkbox on an item, and then set theDocumentText I doubt that's what you want to do, however you could use the webbrowser control as your html document.

If the page is open in thewebBrowser you may use something similar to this:

webBrowser1.Document.GetElementsByTagName("input")[0].InvokeMember("click");

But just the first checkbox will be selected by this.

You may either use server-side java script to check all checkboxes when the first checkbox is chosen or repeat the same code for all input items if you want all checkboxes to be selected.

Once again, if you could be a little clearer about what you want, I could assist you further.

1
5/14/2015 5:43:45 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow