2

How to upload file using python selenium-webdriver when there is no input type but instead it has a button type in HTML?

I'm trying to upload a file to a webpage using Selenium but the HTML type is a button not an Input file.

Below is the HTML Code

enter image description here

Button looks like this

enter image description here

My code

browser.find_element_by_class_name("ng-scope").send_keys('C:\\Users\\Desktop\\test.png')

But after I run the code the file is not uploaded.

Please advise on where i'm going wrong?

Thanks in advance -M

1 Answer 1

5

There is a hidden input with type=file. To upload file using Selenium yo have to send keys to input[type=file]:

browser.find_element_by_css_selector(".file-upload-input input[type=file]").send_keys('C:\\Users\\Desktop\\test.png')

Use WebDriverWait to wait element to be present in the DOM:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# ...

wait = WebDriverWait(driver, 5)

wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".file-upload-input input[type=file]"))).send_keys('C:\\Users\\Desktop\\test.png')
Sign up to request clarification or add additional context in comments.

2 Comments

That works like a charm thank you Sers:) I didn't understand the wait part. Sorry very new to automation. correct me if i'm wrong. It basically waits until the file is uploaded is that correct?
No, it's wait until element input[type=file] will appear in the HTML

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.