5

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/Downloads/1-Students-and-Parent-Email.csv')

But I get the following error:

Traceback (most recent call last):
 File "test.py", line 110, in <module>
 upload_students_results('Surname, Name')
 File "test.py", line 91, in upload_students_results
 file_button.send_keys('/Users/home/Downloads/1-Students-and-Parent-Email.csv')
 File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 349, in send_keys
'value': keys_to_typing(value)})
 File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 493, in _execute
return self._parent.execute(command, params)
 File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 249, in execute
self.error_handler.check_response(response)
 File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 193, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot focus element
 (Session info: chrome=58.0.3029.96)
 (Driver info: chromedriver=2.29.461585 (0be2cd95f834e9ee7c46bcc7cf405b483f5ae83b),platform=Mac OS X 10.12.4 x86_64)
asked May 4, 2017 at 6:48
2
  • Please show how the element with id="fileUploadProxy" is represented in HTML. Commented May 4, 2017 at 6:49
  • @alecxe <div id="fileUploadProxy" class="fileUploadProxy buttonWidget allButtons fileUploadButton">Choose File</div> Commented May 4, 2017 at 6:52

2 Answers 2

9

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:

<input type="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.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
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.

answered May 4, 2017 at 6:54

5 Comments

I think I found it <input type="file" name="fileToUpload" id="fileToUpload2" class="fileToUpload"> so I changed the code to > file_button = browser.find_element_by_id('fileToUpload2') but it is still throwing an error? Also tried adding .click() too.
@Phil which error is thrown now? Is the element visible/editable?
Current error Part A > File "test.py", line 88, in upload_students_results file_button = browser.find_element_by_id('fileToUpload2') File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 282, in find_element_by_id return self.find_element(by=By.ID, value=id_) File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 784, in find_element 'value': value})['value']
Part B File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 249, in execute self.error_handler.check_response(response) File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 193, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"fileToUpload2"}
Thank you, it worked for me. and it also was in iframe. So I switch to by driver.switch_to.frame(browser.find_element_by_name('frame_element'))
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"))

answered May 4, 2018 at 16:43

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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