Skip to content Skip to sidebar Skip to footer

Upload File With Selenium Webdriver Python

I have tried the method from this page: Upload file with Selenium in Python Code: file_button = browser.find_element_by_id('fileUploadProxy') file_button.send_keys('/Users/home/Dow

Solution 1:

The problem is - you are sending keys to the div element which is not "interactable", does not accept the keys - hence the "cannot focus element" error.

The idea behind the solution you've linked is to send keys to the input element with type="file" that is responsible for the file upload. Find this element in your HTML and send keys to it.

Note that this element could be invisible. In this case, you should first make it visible for the send_keys() to work.


Update:

Okay, now we at least know which element is our desired one:

<inputtype="file" name="fileToUpload"id="fileToUpload2"class="fileToUpload">

Since you have troubles locating this element, either try waiting for it:

from selenium import webdriver
from selenium.webdriver.common.byimportByfrom selenium.webdriver.support.uiimportWebDriverWaitfrom selenium.webdriver.supportimport expected_conditions asEC


file_upload = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "fileToUpload2"))
)
file_upload.send_keys('/Users/home/Downloads/1-Students-and-Parent-Email.csv')

Or/and, check if this element is inside an iframe - if it is, you would need to switch into the context of the iframe and only then perform the element search.

Solution 2:

I had the same problem when I inserted the file path as a string. This is functional:file_input.send_keys(os.path.abspath("path/to/the/file.xyz"))

Post a Comment for "Upload File With Selenium Webdriver Python"