Python Mechanize can't find login.live.com form
-
16-03-2021 - |
Question
I'm trying to login to a login.live.com, but Mechanize will not recognize that there is a form on that page. Does anyone have any suggestions?
br=mechanize.Browser()
br.open('https://login.live.com/')
br.select_form(nr=0)
This results in:
mechanize._mechanize.FormNotFoundError: no form matching nr 0
When there clearly is a form on that page.
Solution
Try getting the form by name instead with br.select_form(name="f1")
.
(I got the form name from the page source - I assume you want the login form.)
OTHER TIPS
The problem is that the form is not in the html that is delivered as a response to the HTTP GET request. It's being created later a a result of a javascript script being executed in the browser.
For more information about how to get content that has been dynamically generated, please have a look at the answers to this question.
r = br.open('https://login.live.com/')
r.get_data()
# outputs:
# ...
# Windows Live ID requires JavaScript to sign in. This web browser either does not support JavaScript, or scripts are being blocked
# ...
I found no workaround, and am suggesting you to use Selenium/webdriver instead.