pythonmechanicalsoup

Mechanicalsoup data not being entered into a form


I am trying to enter username and password into a form using mechanicalsoup and it all seems to work correctly but after entering

response = browser.submit_selected()
print(response.text)

It just returns the login page again.

I tried debugging using browser.launch_browser() at the end of my code and it just shows the login page with nothing entered into the username and password fields.

Am I missing something entirely with what I am trying to do? The login page is: https://www.puregym.com/Login/ And my code is below:

import mechanicalsoup


browser.open("https://www.puregym.com/Login/")
browser.select_form('form[id="loginForm"]')


browser["email"] = "test@gmail.com"
browser["pin"] = "12345678"

response = browser.submit_selected()
browser.launch_browser()

Solution

  • It looks like this login page uses JavaScript, which MechanicalSoup does not support. See this FAQ.

    The biggest giveaway is that <form id="loginForm" ...> has no action attribute, which, if it existed, would tell the browser where to submit the form data as an HTTP request in a non-JavaScript context.

    Another way you could tell that the page requires JavaScript is by disabling it in your browser (in Chrome, for example, you can do this with the Chrome Developer Tools) and then attempting to log in. Browsing the site with JavaScript disabled may give you some insight into alternative login approaches, but the likely answer is that you will need to use a tool that supports JavaScript, like Selenium.