1

I am working with robobrowser to login to a website.

The problem I have is that the login page has 2 forms both with the same action, but with different names.

<form action="/login" name="changedomain">
...
</form>

The second form is:

<form action="/login" name="signin">
...
</form>

My python code looks like:

import re
from robobrowser import RoboBrowser
browser = RoboBrowser(history=True, parser="lxml")
browser.open("http://example.com")
form = browser.get_form()

The problem is if I use:

form = browser.get_form()[1]

I get an BadRequestKeyError, and I can't find any documentation on how to locate the form by name. The only documentation says to use the action, but the forms have the same action attribute. any suggestions are welcome.

j-i-l
  • 10,281
  • 3
  • 53
  • 70
dnraikes
  • 275
  • 1
  • 4
  • 14
  • haven't tested this but `get_form` accepts standard BeautifulSoup arguments, so something like this might work: `browser.get_form({'name':'signin'})` – j-i-l Dec 01 '17 at 00:57
  • browser.get_form({'class':'signin'}) and browser.get_form({'class':'.signin'}) don't work (for forms whose only meaningful identifier is a class), so I suspect name doesn't work like this, either. – bobpaul Sep 28 '18 at 15:41

2 Answers2

1

I have working code using the following;

form = browser.get_form('Form1')

Here is another example -- printing the form's html;

from robobrowser import RoboBrowser

browser = RoboBrowser(history=True, parser="html.parser")
browser.open("https://stackoverflow.com")
form = browser.get_form(id="search")
print(form.parsed)

yields

<form action="/search" autocomplete="off" class="searchbar" id="search" method="get" role="search">
<input autocomplete="off" class="f-input js-search-field" maxlength="240" name="q" placeholder="Search…" tabindex="1" type="text" value=""/>
<button aria-label="Search..." class="btn-topbar-primary js-search-submit" type="submit"><svg aria-hidden="true" class="svg-icon iconSearch" height="18" viewbox="0 0 18 18" width="18"><path d="M12.86 11.32L18 16.5 16.5 18l-5.18-5.14v-.35a7 7 0 1 1 1.19-1.19h.35zM7 12A5 5 0 1 0 7 2a5 5 0 0 0 0 10z"></path></svg></button>
</form>
SteveJ
  • 3,034
  • 2
  • 27
  • 47
  • Once selected, can I print the source of the selected form to verify that I have the right form? – dnraikes Dec 01 '17 at 06:04
  • @dnraikes; I updated my answer to show 'print'. My recommendation would be to use a good IDE such as pycharm that allows step debugging. Just by making the browser.open() statement, I can then examine the browser object in the debugger -- and it becomes immediately apparent that the 'parsed' property shows the html. – SteveJ Dec 01 '17 at 15:44
-1

The first answer of how to select the second form is:

form = browser.get_forms()[1] # note the get_forms() not get_form()

the answer to the second question of how to see the source of the form is basically:

print(form) # this prints a dictionary of all input fields and their values from the selected form.
dnraikes
  • 275
  • 1
  • 4
  • 14