Uploading Files via Selenium

Applies to CrossBrowserTesting SaaS, last modified on October 22, 2021

For details about uploading files in Selenium webdriver, see:

https://muthutechno.wordpress.com/2014/07/09/uploading-files-in-selenium-webdriver/

Below is a sample of what your Python script might look like.

from selenium import webdriver
import time

caps = {}
caps['browser_api_name'] = 'Chrome40x64'
caps['os_api_name'] = 'Win8.1'
caps['screen_resolution'] = '1024×768'
caps['record_video'] = 'true'
caps['record_network'] = 'false'
caps['record_snapshot'] = 'false'

USERNAME=""
AUTHKEY=""
CS = "http://" + USERNAME + ":" + AUTHKEY + "@hub.crossbrowsertesting.com:80/wd/hub"
driver = webdriver.Remote(desired_capabilities=caps,command_executor=CS)

time.sleep(5)

driver.get("http://SOMEDOMAIN.com/testupload.html")
time.sleep(5)
e = driver.find_element_by_id("fileuploadField")

# this filename is on your local workstation
e.send_keys("c:\\projects\\2015\\06-jun\\selenium-fileupload\\677px-Mona_Lisa.jpg")

time.sleep(5)

s = driver.find_element_by_id("submit")
s.click()

time.sleep(30)
driver.quit()

See Also

TeamCity Integration
Selenium and Java

Highlight search results