I would like to add a value attribute to multiple input elements. Currently, I am simply using a replace which works but is cumbersome. Is there a way to find an element by its ID attribute and simply add a value attribute?
page = render_template('template.html')
page = page.replace('<input type="text" class="form-control" id="Company" name="Company" placeholder="Company name" data-bs-toggle="tooltip" title="The name of the company that the document is for." onblur="titleCase(this)">',
'<input type="text" class="form-control" id="Company" name="Company" placeholder="Company name" data-bs-toggle="tooltip" title="The name of the company that the document is for." onblur="titleCase(this)" value="' + company + '">')
Is there a way to do this without overcomplicating things?
EDIT: I found this an example in lxml.html documentation that encouraged me to do this:
def fillForm1(page, pDate, company, manager, cAddress, cPhone, cEmail ,sAddress):
form_page = fromstring(page)
form = form_page.forms[0]
form.fields = dict(
pDate=pDate,
Company=company,
Manager=manager,
cAddress=cAddress,
cPhone=cPhone,
cEmail=cEmail,
sAddress=sAddress
)
page = page.replace('<form method="post">', tostring(form, pretty_print=True, encoding='UTF-8', with_tail=False,))
return page
But I keep getting an error about passing a byte back when it should be a string.