I'm trying to login to Dropbox via the official Dropbox API and upload a file to my Dropbox.
The code doesn't seem te click on the submit button in order to login to Dropbox. The code does not stop it just hangs or freezes. I don't get an error so there is no traceback.
What's strange is that when I comment out either the filling of the email or password (or both), clicking the submit button works.
I do not want to manually visit the Dropbox authentication link and click the Allow button. So I'm trying to automate that task by using a tool (Splinter) that let's me automate browser actions.
For my code I'm using Splinter and as browser type I'm using PhantomJS
Here's the code:
from splinter import *
from dropbox import client, rest, session
# Initiate Dropbox API
APP_KEY = '###'
APP_SECRET = '###'
ACCESS_TYPE = 'dropbox'
sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)
emailDropbox = '###'
passwordDropbox = '###'
request_token = sess.obtain_request_token()
urlDropbox = sess.build_authorize_url(request_token)
# Start Splinter login code
# Assumes you are not logged in to Dropbox
# Target url
print 'Target url: ', urlDropbox
browser = Browser('phantomjs')
print 'Starting browser'
print 'Visiting url'
browser.visit(urlDropbox)
# Email form
print 'Is the email form present? ', browser.is_element_present_by_id('login_email')
print 'Fill email form'
browser.find_by_id('login_email').first.fill(emailDropbox)
# Password form
print 'Is the password form present? ', browser.is_element_present_by_id('login_password')
print 'Fill password form'
browser.find_by_id('login_password').first.fill(passwordDropbox)
# Login submit button
print 'Is the submit button present?', browser.is_element_present_by_name('login_submit_dummy')
# Click submit button
print 'Attempting to click the submit button in order to login'
browser.find_by_name('login_submit_dummy').first.click()
print 'Submit button successfully clicked'
# Allow connection with Dropbox
print 'Is the "Allow" button present?', browser.is_element_present_by_id('allow_access')
browser.find_by_id('allow_access').click()
print 'The "Allow" button is successfully clicked'
# Quit the browser
browser.quit()
# The rest of the Dropbox code
# This will fail if the user didn't visit the above URL and hit 'Allow'
access_token = sess.obtain_access_token(request_token)
client = client.DropboxClient(sess)
print "linked account:", client.account_info()
f = open('working-draft.txt')
response = client.put_file('/magnum-opus.txt', f)
print "uploaded:", response
Does anyone have an idea of what's going wrong and how I could fix it?
Thanks.
I got to work by pausing for 5 seconds before clicking using time.sleep(5)
.
Here's the working code:
from splinter import *
from dropbox import rest, session
from dropbox import client as dbclient
import time
# Dropbox API
APP_KEY = '###'
APP_SECRET = '###'
ACCESS_TYPE = 'dropbox'
sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)
emailDropbox = '###'
passwordDropbox = '###'
request_token = sess.obtain_request_token()
urlDropbox = sess.build_authorize_url(request_token)
def phantomjsOAuth():
# Target url
print 'Target url: ', urlDropbox
browser = Browser('phantomjs')
print 'Starting phantomjs browser'
print 'Visiting url'
browser.visit(urlDropbox)
# Email form
print 'Is the email form present? ', browser.is_element_present_by_id('login_email')
print 'Filling email form'
browser.find_by_id('email-field').first.find_by_id('login_email').first.fill(emailDropbox)
print 'Email form successfully filled'
# Password form
print 'Is the password form present? ', browser.is_element_present_by_id('login_password')
print 'Filling password form'
browser.find_by_id('login_password').first.fill(passwordDropbox)
print 'Password form successfully filled'
# Find login submit button
print 'Is the "Submit" button present?', browser.is_element_present_by_name('login_submit_dummy')
submitButton = browser.is_element_present_by_name('login_submit_dummy')
if submitButton == True:
print 'Pausing for 5 seconds to avoid clicking errors'
time.sleep(5)
print 'Attempting to click the "Submit" button in order to login'
browser.find_by_name('login_submit_dummy').first.click()
print '"Submit" button successfully clicked'
# Allow connection with Dropbox
print 'Is the "Allow" button present?', browser.is_element_present_by_css('.freshbutton-blue')
allowButton = browser.is_element_present_by_css('.freshbutton-blue')
if allowButton == True:
print 'The "Allow" button is present, attempting to click..'
browser.find_by_css('.freshbutton-blue').click()
print 'The "Allow" button is successfully clicked, access to Dropbox is granted.'
browser.quit()
dropboxCode()
else:
print 'The "Allow" button is not present, quitting.'
browser.quit()
else:
print 'The "Submit" button was not present, quitting.'
browser.quit()
def dropboxCode():
# The rest of the Dropbox code
# This will fail if 'Allow' wasn't clicked
access_token = sess.obtain_access_token(request_token)
client = dbclient.DropboxClient(sess)
print "linked account:", client.account_info()
f = open('dropbox_api_test_file.txt')
response = client.put_file('/Python/Apps/###/Orders/dropbox_api_test_file.txt', f)
print "uploaded:", response
sess.unlink()
if __name__ == "__main__":
phantomjsOAuth()