dom - get element id using classname with multiple element with same class name in c# -
private void button5_click(object sender, eventargs e) { htmlelementcollection links = webbrowser1.document.getelementsbytagname("input"); foreach (htmlelement link in links) { if (link.getattribute("classname")== "input-style1 psgn-name") { textbox10.text = link.getattribute("id"); } } }
result: id of 4th element id out of 4 element same class. how rest of 3 element id?
your overwriting text value each iteration of loop.
updated code:
private void button5_click(object sender, eventargs e) { htmlelementcollection links = webbrowser1.document.getelementsbytagname("input"); foreach (htmlelement link in links) { if (link.getattribute("classname")== "input-style1 psgn-name") { textbox10.text += link.getattribute("id"); } } }
if wanted put first 4 items found if exist in different textboxes need populate list , refer so:
private void button5_click(object sender, eventargs e) { htmlelementcollection links = webbrowser1.document.getelementsbytagname("input"); list<string> results = new list<string>(); foreach (htmlelement link in links) { if (link.getattribute("classname")== "input-style1 psgn-name") { results.add(link.getattribute("id")); } } textbox10.text = results[0]; textbox11.text = results[1]; etc.... }
using linq makes more elegant solution:
// requires using system.linq
string[] results = (from itm in links itm.getattribute("classname") == "input-style1 psgn-name" select itm.getattribute("id")).toarray();
then populate boxes array elements.
Comments
Post a Comment