简体   繁体   中英

Get HTML nodes that have the same parent - JAVA

I have a document containing several forms similar to the example posted below. I want to extract all the name/value pairs from the hidden input fields of one of the forms, the form is identified by its name and I don't know in advance how many hidden fields will be present.

I am able to select all the relevant input fields in the document using the selector query: input[type=hidden][name][value]

Is there a way to only select the input fields which has FORM[name=lgo] as parent? Using some kind filter maybe?

<FORM METHOD='POST' onSubmit='javascript:isWaitForm();' ACTION='https://abc-azerty.querty.se/carmon/servlet/action/change_1     ' name='lgo'>
    <input type='hidden' name='LogInFlag' value='1'>
    <input type='hidden' name='LogInTime' value='2011-07-26 11:10'>
    <input type='hidden' name='cCode2' value='SE'>
    <a href='javascript:isWaitForm();javascript:document.lgo.submit();' class='linkNone'>Business Monitor</a>
    <a href='javascript:isWaitForm();javascript:document.lgo.submit();' class='linkNone'>
    <input type='image' src='/images/button_arrow_right.gif' height=19 width=22 border=0 style='float:left;'></A>
</FORM>

Based on this info , at least one of following should work -

doc.select("form[name=lgo] > input[type=hidden]");

Or, you can chain your selects -

doc.select("form[name=lgo]").select("input[type=hidden]");

The select method is available in a Document, Element, or in Elements. It is contextual, so you can filter by selecting from a specific element, or by chaining select calls.

<script type="text/javascript">
var inputs = document.getElementsByName('lgo')[0].getElementsByTagName('input');
for(var i = 0 ; i < inputs.length ; i++){
  if(inputs[i].getAttribute('type') == "hidden") {
  // This will get the name: inputs[i].getAttribute('name')
  // This will get the value: inputs[i].value)
  console.log(inputs[i].getAttribute('name') + ": " + inputs[i].value);
}}
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM