c# - How to invoke element in a WebBrowser by the class name(s)? -
i'm trying make simple facebook client. 1 of features should allow user post content on homepage/his profile. logs user in (works fine, of elements have got ids on facebook) , inserts data in corresponding field (works fine well), needs click "post" button. however, button doesn't have id. has got class.
<li><button value="1" class="_42ft _4jy0 _11b _4jy3 _4jy1 selected _51sy" data-ft="{"tn":"+{"}" type="submit">posten</button></li>
('posten' 'post' on german.)
i've been looking around internet few hours , tried different solutions. current solution search item it's inner content ("posten") , invoke it. doesn't work. inserts text doesn't invoke button. here's code:
private void webbrowser1_documentcompleted(object sender, webbrowserdocumentcompletedeventargs e) { if (posthomepage) { webbrowser1.document.getelementbyid("u_0_z").setattribute("value", metrotextbox1.text); getbuttonbyinnertext("posten").invokemember("click"); posthomepage = false; } } htmlelement getbuttonbyinnertext(string searchstring) { string data = webbrowser1.documenttext; //is string contained in website int indexoftext = data.indexof(searchstring); if (indexoftext < 0) { return null; } data = data.remove(indexoftext); //remove text after found text //these strings list of website elements //note: these need updated elements list such as: // http://www.w3.org/tr/rec-html40/index/elements.html string[] strings = { "<button" }; //split string these elements. //subtract 2 because -1 index -1 elements being 1 bigger wanted int index = (data.split(strings, stringsplitoptions.none).length - 2); htmlelement item = webbrowser1.document.all[index]; //if element div (which contains search string //we need next item. if (item.outerhtml.trim().tolower().startswith("<li")) item = webbrowser1.document.all[index + 1]; //txtdebug.text = index.tostring(); return item; }
(this quick solution edited use, not clean). what's wrong here?
it not getbuttonbyinnertext() method searching button element correctly.
here simple replacement try:
htmlelement getbuttonbyinnertext(string searchstring) { foreach (htmlelement el in webbrowser1.document.all) if (el.innertext==searchstring) return el; }
Comments
Post a Comment