vb.net - Extracting the "for" attribute from a label element in an html webpage -
i have code analyses various attributes of webpage when parts of webpage clicked. 1 of elements picked out id of clicked element.
sometimes there no id , instead element being clicked on label using "for" attribute reference id. in these case want pick "for" attribute value.
i have attempted follows:
txtid.text = trycast(myhtmldocument, htmldocument).getelementfrompoint(lastmousepos).getattribute("id") if txtid.text = "" txtid.text = trycast(myhtmldocument, htmldocument).getelementfrompoint(lastmousepos).getattribute("for") end if
for reason .getattribute("for")
returns blank. referencing attribute wrongly - or else going on.
html example below:
<div class="question legal-owner active"> <a class="help-trigger help-trigger-layout"> <span class="help-text-icon"></span> </a> <div class="quote-help quote-help-layout"> <a class="quote-help-close-container"> <div class="quote-help-close"></div> </a> <h3>car ownership</h3> <p> need know whether car belongs you. if don’t own car you’re registered keeper, should answer ‘no’ (the owner of car , registered keeper can different people). </p> </div> <span class="editor-label question-layout"> <label for="owningandusingcarpanel_legalowner">are (or be) legal owner of car?</label> </span> <ul class="question-layout yesno-radio-list"> <li> <input name="owningandusingcarpanel.legalowner" id="owningandusingcarpanel_legalowner_true" type="radio" value="true"> <label for="owningandusingcarpanel_legalowner_true"> <span>yes</span> </label> </li> <li> <input name="owningandusingcarpanel.legalowner" id="owningandusingcarpanel_legalowner_false" type="radio" value="false"> <label for="owningandusingcarpanel_legalowner_false"> <span>no</span> </label> </li> </ul> <span class="editor-validation"> <span class="field-validation-valid" id="owningandusingcarpanel_legalowner_validationmessage"></span> </span> </div>
i have resolved creating own function called getunknown search attribute within tag. should work attribute value surrounded double quotes. function has 2 arguments, first string should contain element tag complete attributes , values, , second attribute want extract value for.
private function getunknown(mytext string, myattr string) dim myresult string = "" dim mystart integer = 0 dim mylen integer = 0 'remove spaces around "=" sign dim mycleantext string = regex.replace(mytext, "\s+([=])\s+|\s+([=])|([=])\s+", "=") 'add =" attribute avoid finding non-attributes when using indexof function dim myfullattr string = myattr.trim().tolower + "=""" try mystart = mycleantext.tolower().indexof(myfullattr) if mystart = -1 myresult = "nothing found" else mystart = mystart + myfullattr.length mylen = mycleantext.indexof("""", mystart) - mystart myresult = mycleantext.substring(mystart, mylen) end if catch ex exception myresult = "nothing found" end try return myresult end function
in context of original question have used follows
dim myelement string = _ trycast(myhtmldocument, htmldocument).getelementfrompoint(lastmousepos).outerhtml.tostring txtid.text = getunknown(myelement, "for")
Comments
Post a Comment