Reputation: 521
i want to fill in a form from a website using following code :
import mechanicalsoup
browser = mechanicalsoup.StatefulBrowser()
browser.open("Web page url")
browser.follow_link("login")
browser.get_url()
browser.select_form('div[class="p30"]')
browser.get_current_form().print_summary()
>>> <input class="form-input" id="mail" type="text"/>
>>> <input class="form-input" id="pass" type="password"/>
as you can see .print_summary() return exact fields that i want to assign values to, but there is no attribute "name" for any of them so i can't change it. I've read Mechanicalsoup tutorial and the form in it has that attribute "name":
<input name="custname"/>
<input name="custtel" type="tel"/>
<input name="custemail" type="email"/>
and it can simply be changed using:
browser["custname"] = "Me"
browser["custtel"] = "00 00 0001"
browser["custemail"] = "[email protected]"
i'm new to mechincalsoup so any help is greatly appreciated.
Upvotes: 1
Views: 170
Reputation: 1280
The mechanicalsoup
Q&A section has specificly answered your question:
If you believe you are using MechanicalSoup correctly, but form submission still does not behave the way you expect, the likely explanation is that the page uses JavaScript to dynamically generate response content when you submit the form in a real browser. A common symptom is when form elements are missing required attributes (e.g. if form is missing the action attribute or an input is missing the name attribute).
In such cases, you typically have two options:
- If you know what content the server expects to receive from form submission, then you can use MechanicalSoup to manually add that content using, i.e., new_control(). This is unlikely to be a reliable solution unless you are testing a website that you own.
2.Use a tool that supports JavaScript, like Selenium.
Upvotes: 1