Skip to content Skip to sidebar Skip to footer

Python File Upload Selenium

I use Selenium and Python to send requests to a website. I am making a file upload right now, but it seems that it is not working as I expect it to be. Here is how to HTML looks li

Solution 1:

You can try the below step as per your requirement which is working well for me related to upload file

Install win32com.client

pip install pypiwin32

Then include it in your code as below

import win32com.client

//click on upload button for uploading file
time.sleep(3)//you can use explicit wait here
shell = win32com.client.Dispatch("WScript.Shell")
shell.Sendkeys("C:\\Users\\.....\\File.pdf")//file path to be uploaded
time.sleep(2)
shell.Sendkeys("{ENTER}")
time.sleep(2)

Solution 2:

Never mind. I just tested this by switching from browser. Instead of using PhantomJS I used Firefox for testing purpose. So I can view what actually is happening behind the scenes. By viewing the browser I came to understand that it needed a couple of seconds to wait in order to handle the file upload handling.

The code:

'''Image Upload'''

upload_photo_element = driver.find_element_by_xpath(upload_photo_field_ID)
image = os.path.join(os.getcwd(), 'images/' + 'img.png')
upload_photo_element.send_keys(image)

# Depending on how long the image takes to load
time.sleep(5)

upload_photo_reject_button_element = WebDriverWait(driver, 10).until(
lambda driver: driver.find_element_by_id(upload_photo_reject_button_ID))

upload_photo_reject_button_element.click()

'''End image upload'''

This code works for me.

In this way no window will pop-up.

But I noticed also that this works with the Firefox browser, but not with PhantomJS.

Post a Comment for "Python File Upload Selenium"