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, 2019 in Python by ADS
• 280 points
2,597 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, 2019 by Abha
• 28,040 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, 2019 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
1,079 views
0 votes
3 answers

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

FWIW, the multiprocessing module has a nice interface for ...READ MORE

answered Dec 15, 2020 in Python by Roshni
• 10,440 points
60,632 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, 2019 in Python by Nymeria
• 3,520 points
4,082 views
0 votes
1 answer

Installing Selenium Webdriver with Python package

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

answered May 8, 2019 in Selenium by Anvi
• 14,150 points
6,165 views
0 votes
2 answers

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,520 points
5,102 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,010 points
5,833 views
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,890 points
3,599 views
0 votes
4 answers