After two days of continuous trial, I thought it's better to ask you folks here. I want to upload a file on my desktop to a remote server. For this I am making use of python and its module requests. I get good help from the internet but even then when I execute my piece of code, I am not able to upload it. So here is the code I have written:
import unittest
from Browser import Browser
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait
import requests
class Upload(unittest.TestCase):
def setUp(self):
print "Setting up before test case execution..."
self.driver = Browser().initialiseBrowser()
self.driver.maximize_window()
def test_Upload(self):
self.driver.get("http://www.justcloud.com")
self.driver.find_element_by_link_text("Login").click()
self.driver.find_element_by_css_selector("input[name='email'][id='login-email']").send_keys("test_usrname")
self.driver.find_element_by_css_selector("input[name='password'][id='login-password']").send_keys("test_pswd")
self.driver.find_element_by_xpath("//input[@value='Login' and @id='login-submit']").click()
wait = WebDriverWait(self.driver, 20)
wait.until(lambda driver: self.driver.title.startswith('Just Cloud'))
try:
self.driver.find_element_by_xpath("//div[@id='not-installed-container']//span[text()='X']").click()
except NoSuchElementException:
print "HURDLE dialog did not appear"
url = "https://upload.backupgrid.net/add"
fileToUpload = {'file':open('/home/pinku/Desktop/Test_Upload.odt', 'rb')}
print fileToUpload
s = requests.Session()
s.auth = ('test_usrname', 'test_pswd')
response = s.post(url, files=fileToUpload)
print response.text
def tearDown(self):
driver.close()
Please help me point out the mistake I might have made in the code above. 2 days back I had absolutely no clue of the approach to upload a file. I am sure I am very near to this solution. So I request you guys to help me get it. Trust me, I have put a lot of effort to get this done.
{'file': <open file '/home/pinku/Desktop/Test_Upload.odt', mode 'rb' at 0x9267b20>} <requests.packages.urllib3.response.HTTPResponse object at 0x929d5cc>
url = "https://upload.backupgrid.net/add" fileToUpload = {'file':open('/home/pinku/Desktop/Test_Upload.odt', 'rb')} print fileToUpload s = requests.Session() s.auth = ('test_usrname', 'test_pswd') response = s.post(url, files=fileToUpload) print response.text