How to automate image upload from my computer to site using python-selenium?

+1 vote

Hello,

I want to upload an image on one of the sites. Image is on my C drive. How to automate it using python selenium.

I used following but it's not working

driver.find_element_by_id("button_desktop").send_keys(os.getcwd()+"C:/Desktop/MyImage.png")

Please suggest.

Thanks in Advance

ADS

Aug 12 in Python by ADS
• 280 points
45 views

2 answers to this question.

0 votes

Hi, please try using following script to automate image upload from computer:

from selenium import webdriver 

driver = webdriver.Firefox()
driver.implicitly_wait(15) 
driver.get("http://example.com/file-upload-page") 
driver.find_element_by_id("id-of-uploadfile-element").send_keys("file-path") 
driver.find_element_by_id("submit").click()
answered Aug 13 by Abha
• 25,240 points
Thanks Abha,

But its not working. I already tried with this.
Can you tell what error you are getting?
0 votes

Hey, this code worked for me:

image_path=os.path.abspath('.\\folder1\\subfolder2\file1.jpg')
driver.find_element_by_id("Id of the element").clear()            
driver.find_element_by_id("Id of the element").send_keys(image_path)
answered Aug 19 by Rishi
Hi Rishi, your answer looks pretty good. use of clear method() is also wise as it will remove any previous text written to the web element.

Related Questions In Python

0 votes
1 answer

How to get text label from SAP using pywinauto[python]

Hi. Can you please tell me what ...READ MORE

answered Jun 28, 2018 in Python by Nietzsche's daemon
• 4,260 points
417 views
0 votes
1 answer

How to get the return value from a thread using python?

You don't need to change your existing ...READ MORE

answered Dec 3, 2018 in Python by Nymeria
• 3,520 points

edited Dec 19, 2018 by Nymeria 12,294 views
0 votes
1 answer

How to use read a WSDL file from the file system using Python suds?

Hi, good question. It is a very simple ...READ MORE

answered Jan 21 in Python by Nymeria
• 3,520 points
586 views
0 votes
1 answer

Installing Selenium Webdriver with Python package

Hey Hemant, for installing Selenium Webdriver with ...READ MORE

answered May 8 in Selenium by Anvi
• 13,520 points
176 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,090 points
780 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
1,025 views
0 votes
4 answers