nousername46291772
nousername46291772

Reputation: 3

Python - not able to extract value of hidden input

I am trying to extract the value of a hidden input tag. Even though the element exists in the HTML i can't find it with bs4.

This is the error message i get:

AttributeError: 'NoneType' object has no attribute 'find'

This is the html on the webpage:

<form id="exampleid" class="exampleclass" action="/ex/ex-ex/ex/2" method="post">
    
    <more html>
                                
    <div>
    <input type="hidden" name="csrf" value="abcdefghijklmnopqrstuvwxyz">
    </div></form>

And this is my current code:

csrf = soup.find("form", {"id": "exampleid"})
csrf = csrf.find('input', {'name': 'csrf'}).get("value")
print(csrf)

I would appreciate any kind of help as it is really bothering me. Thank you in advance!

Upvotes: 0

Views: 419

Answers (1)

HedgeHog
HedgeHog

Reputation: 25048

Your selection is still working, think there is another issue, maybe you wont get the html you expect.

As alternativ to select and get the value of this hidden <input> you can use the following css selector:

soup.select_one('#exampleid input[name*="csrf"]')['value']

Example

from bs4 import BeautifulSoup

html = '''
<form id="exampleid" class="exampleclass" action="/ex/ex-ex/ex/2" method="post">
<div>
<input type="hidden" name="csrf" value="abcdefghijklmnopqrstuvwxyz">
</div></form>'''

soup = BeautifulSoup(html, "lxml")

csrf = soup.select_one('#exampleid input[name*="csrf"]')['value']

print(csrf)

Output

abcdefghijklmnopqrstuvwxyz

Upvotes: 1

Related Questions