1

How do I set an input element to read only with beautiful soup? my element looks like this:

<input id="province" value="BC" />

I know you can create the new attribute in the tag like you would create a new key for a dictionary but this isn't working:

soup.find(id="province")["readonly"]

Solution

soup.find(id="province")["readonly"] = None
wpercy
  • 9,636
  • 4
  • 33
  • 45

1 Answers1

1

First select the tag, then add the readonly attribute with a value of readonly (or any other value, see here).

It should look something like this:

tag = soup.find(id="province")
tag['readonly'] = 'readonly'

If you want to add the readonly attribute to all of the elements with that id, use this:

tags = soup.find_all(id="province")
for tag in tags:
    tag['readonly'] = 'readonly'
Community
  • 1
  • 1
jlbnjmn
  • 958
  • 7
  • 5