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.
Related
I am trying to upload image file into the browser using mechanize.
Although there is no error, the uploaded file does not reflect when I check manually in the browser (post submit/saving).
I am using the following code to upload the files
import mechanize as mc
br = mc.Browser()
br.set_handle_robots(False)
br.select_form(nr=0)
br.form.add_file(open("test.png"), content_type="image/png",
filename='before',name="ctl00$ContentPlaceHolder1$fileuploadBeforeimages")
br.submit("ctl00$ContentPlaceHolder1$cmdSave")
# this is supposed to save the form on the webpage. It saves the texts in the other fields, whereas the image does not show up.
The add file command seems to work. I can confirm this because when I print br.forms()[0] the file details show up (<FileControl(ctl00$ContentPlaceHolder1$fileuploadBeforeimages=before)>).
But there is no sign of the image file post this code snippet. I have checked several examples which include br.submit() without any specific button control, when I do this no page is saved on the website.
What am I missing?
Thanks in advance.
EDIT
When I manually try to upload the file, I see a pop-up asking for confirmation. Under inspect, this is present as
onchange="if (confirm('Upload ' + this.value + '?')) this.form.submit();"
I am not sure if this is a JavaScript element and mechanize cannot pass through this part for upload function. Can someone confirm this.?
you can just put 'rb' in front of image name like this:
br.form.add_file(open("test.png",'rb'),'images/png',filename,name='file')
I have a form that has to be filled and I have written the code using mechanize to fill the form details and submit it. Everything is working fine as expected and I'm able to submit it and I'm also getting the confirmation as Form Submitted Successfullyin the next page.
Now, I have to read this content, the Status- Form Submitted Successfully or if the attempt is not successful - Form NOT Submitted Successfully from the next page (page after submission of the form) along with other details like my name, form number, form name and status which all are displayed in one line.
DEV D 0911 Scientific_Conference Form Submitted Successfully
After the code - res = br.submit(), how do I read all these contents of the next page and display it in my output. What should I do to capture the next page contents and display accordingly? Any suggestions would be appreciated. Kindly help me in understanding this process. Thanks in advance.
I'm not even able to display the html contents.
Code I used (but not working):
res = br.submit()
final = res.geturl()
print final
x = br.open(final)
print "Loading...\n"
print (x.read())
print "program ended"
I want to submit a protein sequence online for HMM comparison using HHpred tool and collect its job id so that I can collect the output later and process. But I have 1200 such sequences and I need to automate the process very badly. I tried to use mechanize package in Python but I couldn't understand properly as there is no documentation available.
import mechanize
ur = "http://toolkit.tuebingen.mpg.de/hhpred/"
request = mechanize.Request(ur)
response = mechanize.urlopen(request)
forms = mechanize.ParseResponse(response, backwards_compat=False)
print response.code
form = forms[0]
print form
original_text = form["jobid"]
form["jobid"] = '''MNDKSKNMMKNFIRTFAGLLLAILLILGFFLLVFPKAGDRFLADKKVSTLSAKNLTYAALGDSLTEGVGDATGQGGF VPLFAKDIENKTDSSVSSQNFGKAGDTSTQIYNRMMKSKKITDGLKKADIITITIGGNDVLKV
IRDNVSKLSSMTEKDFTKPEELYQARVKKLLDKIREDNPKAQIYVLGIYNPFYLNFPELTVMQNVIDSWNTATAGVVSQE KNTYFIPINDLLYKGSGDKQAVESGSTSDAVSNNLLYTEDHFHPNNVGYQLMADAVFASY
KEVNQK'''
control = form.find_control("jobid")
print control.name, control.value, control.type
control = form.find_control("showres")
print control.name, control.value, control.type
print control.disabled
request2 = form.click("showres")
response2 = mechanize.urlopen(request2)
forms2 = mechanize.ParseResponse(response2, backwards_compat=False)
form2 = forms2[0]
print form2
The website http://toolkit.tuebingen.mpg.de/hhpred/ has many input fields but i could see only the "jobid" and "showres" in the control list using 'mechanize' parser. Code above is what i tried to do but its totally incorrect.
I actually want to paste the sequence in the text box and hit on submit and if possible give my own job-id at the bottom. And save the url of the resulting page after hitting submit.
Kindly help me. (I'm using windows)
I am using mechanize to automate some form submissions.
To do that I require to go to the home page of some website, click on a link with a particular text which redirects me to an another page and fill in the form in the new page.
I tried using
response = br.follow_link(text_regex="sometext")
for f in response.forms()
print f.name
The error Message I got was AttributeError: closeable_response instance has no attribute 'forms'
When I tried
for f in br.forms()
print f.name
It prints the name of forms in the hompage and not the new page I redirect to .
How can find the name of the forms in the new page ?
What does 'response' contain ?
And what is the difference between click_link() and follow_link() . The mechnanize doc doesn't explain this clearly.
Thanks
for the difference between click_link() and follow_link():
both methods take the same keywords as parameters.
click_link() is a method which will return a Request object, which then can be used for creating a request:
req = br.click_link(text='Sample Text')
br.open(req)
follow_link() will perform the same action as .open(), directly opening the link.
This information has been taken from the following documentation:
http://joesourcecode.com/Documentation/mechanize0.2.5/mechanize._mechanize.Browser-class.html#click_link
Follow_link() behaviour can be observed in the examples given at wwwsearch:
http://wwwsearch.sourceforge.net/mechanize/
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.