I have been working on this for couple days now. Can not really find how to make this work. I am fairly new to aspx websites and fetching information out of them.
I am trying to login/authenticate on a website that uses aspx pages. So I followed this thread which really helped me get this in motion. (Last Answer)
Following those directions, I write:
url = "http://samplewebsite/Main/Index.aspx" # Logon page
username = "user"
password = "password"
browser = RoboBrowser(history=True)
# This retrieves __VIEWSTATE and friends
browser.open(url)
signin = browser.get_form(id='form1')
print(signin)
This is the outcome of that print statement:
<RoboForm __VIEWSTATE=/wEPDwULLTE5ODM2NTU1MzJkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYBBQlidG5TdWJtaXRriD1xvrfrHuJ/0xbQM08yEjyoUg==, __VIEWSTATEGENERATOR=E78488FE, adminid=, btnSubmit=, pswd=>
So it is obvious that I am retrieving the information correctly. Now I have 3 input fields:
adminid
btnSubmit
pswd
Which I can use in the following manner:
signin["adminid"].value = username
signin["pswd"].value = password
signin["btnSubmit"].value = "btnSubmit.x=29&btnSubmit.y=22"
My only problem is the last field btnSubmit which I do not know how to input a value since this is of the following type:
<input type="image" name="btnSubmit" id="btnSubmit" tabindex="3" src="../image/login_btn.gif" style="height:41px;width:57px;border-width:0px;" />
when I submit on the website, using the Chrome Tools I get the following outcome:
__VIEWSTATE:/wEPDwULLTE5ODM2NTU1MzJkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYBBQlidG5TdWJtaXRriD1xvrfrHuJ/0xbQM08yEjyoUg==
__VIEWSTATEGENERATOR:E78488FE
adminid:user
btnSubmit.x:23
btnSubmit.y:15
pswd:password
Where basically the x,y positions are where I clicked on the page. Really do not know how to do this request through Python. Used this to no avail.
When you click on an input object of type image, two form values are set, the button name plus .x for the first, and .y for the other.
However, pressing Enter in a regular text input field will also submit a form, so you don't have to click on a submit button. I'd just leave the value empty altogether.
There is not much flexibility in the way robobrowser handles form submits, to avoid using the submit button you'd have to delete it from the form outright:
del signin.fields['btnSubmit']
before submitting.
If you must submit using the image button, then you'll have to teach Robobrowser how to handle that type; currently it has no handling for these. The following adds that:
from functools import wraps
from robobrowser.forms import form
from robobrowser.forms.fields import Submit, Input
class ImageSubmit(Submit):
def serialize(self):
return {self.name + '.x': '0', self.name + '.y': '0'}
def include_image_submit(parse_field):
#wraps(parse_field)
def wrapper(tag, tags):
field = parse_field(tag, tags)
if type(field) is Input: # not a subclass, exactly this class
if field._parsed.get('type') == 'image':
field = ImageSubmit(field._parsed)
return field
return wrapper
form._parse_field = include_image_submit(form._parse_field)
at which point you can use browser.submit_form(signin, signin['btnSubmit']) to submit the form and the correct fields will be included.
I've submitted a pull request to the robobrowser project to add image submit support.
Related
I am trying to make a script that auto fills the HTML5 form values (Name and Password). I don't know how to insert the values.
On other sites I can't find a good explanation about how to do it.
Login site: https://ncod22.n-able.com/
username = TestName
password = TestPwd
(These aren't working)
Pyton code (so far):
import webbrowser, sys
webbrowser.open('https://ncod22.n-able.com/')
Try mechanize:
import mechanize
url="https://ncod22.n-able.com/"
pg=mechanize.Browser()
pg.set_handle_robots(False)
r=pg.open(url) #open page
pg.select_form(nr=0) #select form number
pg.form["username"]="TestName" #<input> name
pg.form["password"]="TestPwd" #<input name="password">
pg.method="POST" #form method
r=pg.submit() #submitting form
#print(r.read()) to see Page source
I am trying to login to a site with mechanicalsoup, but when I submit the form, it keeps me on the same page. I have done a lot of researching for this and could not find an answer.
br.open(domain + action)
form = br.select_form()
user_info = getUserInfo()
br["ff_login_id"] = user_info["eid"]
br["ff_password"] = user_info["password"]
br["empl-login_submit"] = "SUBMITTED"
br.get_current_form().print_summary()
res = br.submit(form, domain)
print(res) #This is getting a response 200
If you have used Mechanize to do this in the past, then it should be possible with MechanicalSoup, unless the website has changed.
Depending on what action is, this could be an issue with the URL passed to submit. The preferred method to submit forms is:
res = br.submit_selected()
This will ensure that you are passing the correct URL to the submit function. Perhaps give that a try to see if it solves your problem.
I'm using Python Mechanize for adding an event to WordPress but I can't seem to figure out how to write to the TinyMCE Editor in the 'Add New' Event section.
I've been able to make a draft so far by just setting the Title with some value for testing purposes but I am stuck here. What I've done so far is...
br = mechanize.Browser()
response = br.open(url)
Intermediate steps to get to the correct page that don't need to be listed...
Once on the correct page I choose the form that I want to work with, select it and set the title. Once I submit I can actually travel to my drafts section in my normal chrome/firefox browser to see a draft has been created.
for f in br.forms():
if f.name == postForm:
print f
br.select_form(f.name)
br.form['post_title'] = 'Creating from MECHANIZE'
br.submit(name='save', label='Save Draft')
What would be the intermediary steps to input data into the TinyMCE editor?
I realized that by writing:
br.form['content'] = "some content"
You are able to write to the custom textarea. Any HTML content that you have in triple double-quotes will show up as you want once you submit the post.
I have a scenario file that describes logging into an account;
Scenario: Failed login with blank login details
Given I go to "BBC Login Page"
When I fill in the username textfield with ""
And I fill in the password textfield with ""
And I press "Log in"
Then I should see "In order to login, you must enter a valid user name."
In my step definitions (Python using Lettuce), which would fail unless I pass in the URL in my scenario (bad BDD);
#step('I go to "(.*?)"$')
def go_to(step, url):
with AssertContextManager(step):
world.browser.get(url)
Instead I want to build in a little bit of logic that substitutes the path for the real URL;
msr_login_page = "https://www.bbc.co.uk/login"
#step('I go to "(.*?)"$')
def go_to(step, url):
if url == "BBC Login page":
urladjusted = msr_login_page
with AssertContextManager(step):
world.browser.get(urladjusted)
This fails with an error, and I don't appear to be able to set the URL variable at all no matter how I try to set it.
Thanks in advance for any help
Why not use a dictionary mapping for your page URLs by name?
urls = {"BBC Login page": "https://www.bbc.co.uk/login"}
#step('I go to "(.*?)"$')
def go_to(step, page):
with AssertContextManager(step):
world.browser.get(urls[page])
I've been writing a program to log in to Facebook and update the status as a side project. I managed to get the program to login. However, I'm having trouble selecting the textarea that ends up being the "Enter your status here" box. Using "Inspect Element" in Chrome, I'm able to see the form under which it's located, but listing the forms in the program doesn't seem to list said form...
import mechanize
import re
br = mechanize.Browser()
usernamecorrect = 0
while usernamecorrect == 0:
username = raw_input("What is the username for your Facebook Account? ")
matchmail = re.search(r'[\w.-]+#[\w.-]+', username)
if matchmail:
print matchmail.group()
usernamecorrect = 1
else:
print "That is not a valid username; please enter the e-mail address registered with your account.\n"
password = raw_input("What is the password for your account?")
print "Logging in..."
br.set_handle_robots(False)
br.open("https://www.facebook.com/")
br.select_form(nr = 0)
br['email'] = username
br['pass'] = password
br.submit()
raw_input("Login successful!")
print "Forms: \n"
for f in br.forms():
print f.name
The full output is as follows:
What is the username for your Facebook Account? myemail#website.com
What is the password for your account? thisisapassword
Logging in...
Login successful!
Forms:
navSearch
None
I took a look through the source of Facebook via Inspect Elements again, and "navSearch" is the "Find People, things, etc." search bar, and the unnamed form appears to have to do with the logout button. However, while Inspect Elements gives at least 2 more forms, one of which holds the status update box. I haven't been able to determine if it's because of JavaScript or not (while the status update box code block is encapsulated in , so are the navSearch and logout forms.) The most relevant thing I've been able to find is that navSearch and the logout forms are in a separate div, but I somehow feel as though that shouldn't be much of a problem for mechanize. Is there just something wrong with my code, or is it something else entirely?
Is there just something wrong with my code, or is it something else entirely?
Your whole approach is wrong:
I've been writing a program to log in to Facebook and update the status
That’s what the Graph API is for.
Scraping FB pages and trying to act as a “browser” is not the way to go. Apart from the fact, that FB policies do not allow that, you see how difficult it gets on a page that uses JavaScript/AJAX so much.
Go with the API, it’s the easy way.