1

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"] = "nobody@example.com"

i'm new to mechincalsoup so any help is greatly appreciated.

  • Possible duplicate of [Using mechanicalsoup to set value of form element w/o a name](https://stackoverflow.com/questions/52585753/using-mechanicalsoup-to-set-value-of-form-element-w-o-a-name) – Matthieu Moy Jun 07 '19 at 17:38

1 Answers1

1

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:

  1. 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.

Masoud
  • 1,270
  • 7
  • 12
  • Probably the right answer, yes: a field without a `name=` attribute is useless without JavaScript, so there's almost certainly some JavaScript code in the HTML page, and MechanicalSoup won't help. – Matthieu Moy Jun 07 '19 at 17:37
  • @masoud Thanks , is selenium a better tool to work with ? I'm newbie and i just want to make sure that I'm going to use the best tool out there! – Saeid Mohammadi Nejati Jun 07 '19 at 21:14