Can not submit a form with robobrowser. Invalid submit error - python-3.x

Here is my code:
from robobrowser import browser
url = 'http://diesel.elcat.kg/index.php?act=Login&CODE=00'
url3 = 'http://diesel.elcat.kg/index.php?act=post&do=reply_post&f=178&t=233500064'
m = browser.RoboBrowser()
m.open(url)
# SIGNING IN(form1)
form1 = m.get_form(action='https://diesel.elcat.kg/index.php?act=Login&CODE=01')
form1['UserName'].value = 'Username'
form1['PassWord'].value = 'Password'
m.submit_form(form1)
# FINISHED SIGNING IN(everything worked)
# GOING TO THE PAGE WHERE FORM IS LOCATED
m.open(url3)
# Can't submit this form
form2 = m.get_form(action="http://diesel.elcat.kg/index.php?")
form2['Post'].value = 'up'
m.submit_form(form2)
I can sign in to the website so form1 works, but when I try in this case leave a comment(up), form2 does not work.I am keep getting either InvalidSubmit error either Bad Request error. Code of form1 and code of form2 seem to be the same, but one works and another does not. I am using python3.5 and robobrowser, and I am using Mac OS if that's gonna help. Thank you in advance.
Here is my traceback:
Traceback (most recent call last):
File "/Users/bkkadmin/Desktop/Daniiar/upper/test2.py", line 18, in <module>
m.submit_form(form)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/robobrowser-0.5.3-py3.5.egg/robobrowser/browser.py", line 339, in submit_form
payload = form.serialize(submit=submit)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/robobrowser-0.5.3-py3.5.egg/robobrowser/forms/form.py", line 226, in serialize
include_fields = prepare_fields(self.fields, self.submit_fields, submit)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/robobrowser-0.5.3-py3.5.egg/robobrowser/forms/form.py", line 154, in prepare_fields
raise exceptions.InvalidSubmitError()
robobrowser.exceptions.InvalidSubmitError

I experienced the same issue today, with the same exact errors. One possible cause of the above issue is that your form2 actually contains more than one submit field, corresponding to more than one submit button in the original html. You can check this by print(len(list(form2.submit_fields.items(multi=True)))). If this is the case, your call to submit_form has to be modified as m.submit_form(form2, submit=your_submit), where the second argument your_submit is the relevant submit field you want to use. This reference discusses how to extract the submit field you desire.
Incidentally, if you wonder where the long print code comes from, it comes from the body of prepare_fields in robobrowser/forms/form.py, which is indicated in one of the error outputs you posted.
Hope this helps!

It happens when the package you installed courrupted in your current project...
So try to create a new project in pycharm. And install robobrowser library again....
And paste your file in new project...
Noteuse this method only if your code is completely correct 😉

Related

How to add a new method to an existing import in python? Specifically moviepy

For whatever reason, Python is not allowing me to access a custom method I created in moviepy's preview.py file. I just want to know how to correctly implement it into the file. For reference, before I changed the name of the method, it was working correctly.
I checked at least two __init.py__ files and they were effectively empty. I couldn't find if methods are initialized anywhere, and is probably what I'm missing.
I also tried restarting Git Bash and that didn't work either (another solution I saw).
Original:
#convert_masks_to_RGB
def preview(clip, fps=15, audio=True, audio_fps=22050, audio_buffersize=3000,
audio_nbytes=2, fullscreen=False):
Changed:
#requires_duration
#convert_masks_to_RGB
def preview_custom(clip, marker_overlay="marker_overlay.png", fps=15, audio=True, audio_fps=22050, audio_buffersize=3000,
audio_nbytes=2, fullscreen=False):
There are more than a few differences between the changed and original method, however at the moment the only result I expect is having the method be called correctly. Error is below:
Traceback (most recent call last):
File "T3AJM.py", line 249, in <module>
main()
File "T3AJM.py", line 34, in main
GUI_main_menu()
File "T3AJM.py", line 85, in GUI_main_menu
GUI_play_markers()
File "T3AJM.py", line 125, in GUI_play_markers
video.preview_custom(marker_overlay=TEMP_OVERLAY_FILE)
AttributeError: 'VideoFileClip' object has no attribute 'preview_custom'
Thank you for your time.
I'm not even sure if this technically fixes the problem, but just doing:
from moviepy.video.io.preview import *
and
preview_custom(video, marker_overlay=TEMP_OVERLAY_FILE)
fixed the problem. I have no idea why I had to change the way it was called, as doing clip.preview(), or in this case video.preview() worked perfectly fine before, but whatever.

Has anyone tried running macro embedded in a microsoft word document from Python

I have a macro in word 2013, that removes white spaces and adjust page width size to ensure the tables fit.
Currently, in order to make these changes to the document, the user needs to run the macro first, and only after running it, when they print the document, the changes are ensured.
I would like to automate the part of running the macro
This is for formating the word doc to ensure that the tables in the document fit the page.
import os
import time
import win32com.client
from docx import Document
macro_to_run = 'PostProcess'
document =
wordapp.Documents.Open('C:\\Users\\sarvesa\\Downloads\\test_xrd.doc')
wordapp.run(document, macro_to_run)
document.save
document.close
Traceback (most recent call last):
File "filename_change.py", line 12, in
wordapp.run(document, macro_to_run)
File "C:\Users\sarvesa\AppData\Local\Programs\Python\Python36-32\lib\site-packages\win32com\client\dynamic.py", line 516, in getattr
ret = self.oleobj.Invoke(retEntry.dispid,0,invoke_type,1)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Word', "'Run' is not a property.", 'wdmain11.chm', 25342, -2146822426), None)
provided "wordapp" is the instance of word, called like this:
wordapp = win32com.client.DispatchEx("Word.application")
then there are 2 issues:
1) you are missing "Application"
2) These methods are all inherited from VBA, which means you need to use that syntax. This includes casing!
I too had to google a lot of this information and compile it to a working script. I recommend reading some VBA tutorials as well.
Try:
wordapp.Application.Run(macroName)
Edit:
Also your "macro_to_run" might be insufficient.
You need to not only specify the module name, but also the Sub you want to run:
Like "Normal.Module1.SubName"

Unable to login into site through RoboBrowser Python

I tried to login into the site through RoboBrowser but I'm facing below error.
import config
from robobrowser import RoboBrowser
br = RoboBrowser()
br.open('https://stackoverflow.com')
form = br.get_form(id="login-form")
form['Email address']=config.username
form['Password']=config.password
br.submit_form(form)
Error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\test\AppData\Local\Programs\Python\Python37\lib\site-packages\robobrowser\forms\form.py", line 216, in __setitem__
self.fields[key].value = value
File "C:\Users\test\AppData\Local\Programs\Python\Python37\lib\site-packages\werkzeug\datastructures.py", line 784, in __getitem__
raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
Through any other module, we are able to login into site and search data in the next screen. Example if I login into site and next page it will display as "Hello User".
I want to bring the output and see "Hello User" is displayed or not through the python script
I think you are trying to login to the sing up form. Move to the login page and then login.
import config
from robobrowser import RoboBrowser
br = RoboBrowser()
br.open('https://stackoverflow.com/users/login')
form = br.get_form(id="login-form")
form['email']=config.username
form['password']=config.password
br.submit_form(form)
This error, surprisingly, would happen if you are setting a form field which is not actually a part of the form. Tomasz's answer goes to the point of what was wrong, but a couple additional notes:
robobrowser is not supported and you should look into better-maintained libraries like MechanicalSoup or mechanize
StackExchange has an API

Python - LinkedIn API get profile error

This is for an internal project. My end goal is to get details of my connections. who are in same city as I am.
I am new in using LinkedIn API . I have used code mentioned in answer here to generate the access token. Now I am using below line to get my LinkedIn profile.
application.get_profile(access_token['oauth_token'])
But I am getting below error.
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
application.get_profile(access_token['oauth_token'])
File "C:\Python34\lib\site-packages\python_linkedin-2.0-py3.4.egg\linkedin\linkedin.py", line 189, in get_profile
response = self.make_request('GET', url, params=params, headers=headers)
File "C:\Python34\lib\site-packages\python_linkedin-2.0-py3.4.egg\linkedin\linkedin.py", line 169, in make_request
params.update({'oauth2_access_token': self.authentication.token.access_token})
AttributeError: 'str' object has no attribute 'token'
Can someone please help me?
It is a bit late I guess. But for future reference, this error comes from the fact that you need to call explicitly the token element from the linkedin application.
For instance, to generate the application, you need to enter the following:
from linkedin_v2 import linkedin
application = linkedin.LinkedInApplication(token=access_token['oauth_token'])
From there, you can then call the different function from application (like "application.get_profile()").
BR
I don't think you need to pass the auth token again to get your profile. I am also using the python lib.
application.get_profile()
Should return the basic information for your profile. Then look into using selectors to specify what endpoints you want.

Proper way to cleanup dynamic engines and can they be loaded twice?

I am having problems loading Engine PKCS #11 as a dynamic engine using python and M2Crypto. I am trying to access an Aladdin USB eToken.
Here are the important steps from my python code:
dynamic = Engine.load_dynamic_engine("pkcs11", "/usr/local/ssl/lib/engines/engine_pkcs11.so")
pkcs11 = Engine.Engine("pkcs11")
pkcs11.ctrl_cmd_string("MODULE_PATH", "/usr/lib/libeTPkcs11.so")
pkcs11.engine_init_custom() # initialize engine with custom M2Crypto patch
# next few steps which I deleted pass password and grab key & cert off token
Engine.cleanup()
This works fine the first time this method gets run. The second time, it fails when loading the dynamic engine (see error below).
Traceback (most recent call last):
File "", line 1, in ?
File "/usr/local/lib/python2.4/site-packages/M2Crypto/Engine.py", line 98, in load_dynamic_engine
e.ctrl_cmd_string("LOAD", None)
File "/usr/local/lib/python2.4/site-packages/M2Crypto/Engine.py", line 38, in ctrl_cmd_string
raise EngineError(Err.get_error())
M2Crypto.Engine.EngineError: 4002:error:260B606D:engine routines:DYNAMIC_LOAD:init failed:eng_dyn.c:521:
Is it impossible to load engines twice in a python session? Am I missing some kind of engine cleanup/deletion? The OpenSSL docs talk about engine_finish() but I don't think M2Crypto offers that. Is there a method to tell if the engine is already loaded?
Thanks!
M2Crypto does have ENGINE_finish and ENGINE_free available in the svn trunk version. The Engine class has init, and finish methods, and when an instance gets deleted it will be free'd. Can you give that a try? If you see any issues there is still time to fix them for next release.
My python code displayed nicer than it is in the comment section. The pkcs11.finish() method causes a segmentation fault in M2Crypto revision 723.
dynamic = Engine.load_dynamic_engine("pkcs11", "/usr/local/ssl/lib/engines/engine_pkcs11.so")
pkcs11 = Engine.Engine("pkcs11")
pkcs11.ctrl_cmd_string("MODULE_PATH", "/usr/lib/libeTPkcs11.so")
pkcs11.init()
# next few steps which I deleted pass password and grab key & cert off token
pkcs11.finish()
Engine.cleanup()
Anyone have advice on whether I'm doing something wrong or if there is a problem with the M2Crypto code?

Resources