Question

<input name="utf8" type="hidden" value="&#x2713;" />
<input name="ohboy" type="hidden"  value="I_WANT_THIS" />
<label for="user_email">Email</label>
<input class="form-control" id="user_email" name="user[email]" size="30" type="email" value=""      />

I'm kinda stuck here, I was originally going to use find() instead of xpath() because the tag input is in several places in the source, but i figured out that find() only returns the first occurence in the source

Was it helpful?

Solution

Use find(), passing the xpath expression specifying an integer index of an element:

from lxml.html import fromstring


html_data = """<input name="utf8" type="hidden" value="&#x2713;" />
<input name="ohboy" type="hidden"  value="I_WANT_THIS" />
<label for="user_email">Email</label>
<input class="form-control" id="user_email" name="user[email]" size="30" type="email" value=""      />"""

tree = fromstring(html_data)
print tree.find('.//input[2]').attrib['value']

prints:

I_WANT_THIS

But, even better (and cleaner) would be to find the input by name attribute:

print tree.find('.//input[@name="ohboy"]').attrib['value']
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top