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
5,652 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 14, 2019 by Abha
• 28,140 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
2,290 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,520 points
105,133 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,560 points
7,670 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
15,139 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,500 points
12,616 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,190 points
8,571 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,910 points
6,629 views
0 votes
4 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP