3

I'm writing an automated test that is supposed to import a series of files through the UI of a web application. You click the "Import" button and it opens Windows Explorer. I use the following XPATH in my test:

filename_field = //div[contains(@class, 'DocumentGrid')]//input[contains(@type, 'file')]

I'm getting an InvalidElementStateException.

Message: invalid element state: Element is not currently interactable and may not be manipulated

Here is a sample of my test code:

element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, Tags.filename_field))

element.send_keys(Keys.importFilePath)

I'm not sure why I'm getting this error. This code worked with a different web application, but this one I'm working on now was built using the React framework and the other was not. Not sure if that makes a difference.

Trevor
  • 160
  • 1
  • 12
  • I remember a while ago I had problem with selenium timer and it wasn't working properly. Try `time.sleep(10)` and then find element normally to be sure if it is selenium bug. – Ali Ent May 10 '23 at 22:53
  • You should probably include the HTML markup of this input element. – pcalkins May 11 '23 at 23:02

2 Answers2

1

The file input element is not interactable because it is hidden, disabled or not visible, just use JavaScript to manipulate the hidden file input element. Below is an example, be sure that driver is your WebDriver instance and to replace path/to/your/file with the actual path to the file you want to upload.

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

filename_field_xpath = "//div[contains(@class, 'DocumentGrid')]//input[contains(@type, 'file')]"

element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, filename_field_xpath)))

file_path = "path/to/your/file"
driver.execute_script(f"arguments[0].style.display = 'block'; arguments[0].value = '{file_path}';", element)

driver.execute_script("arguments[0].dispatchEvent(new Event('change'));", element)
Saxtheowl
  • 4,136
  • 5
  • 23
  • 32
1

I faced some similar issues before.

For me it was another element blocking the element i wanted to click. In one case the cookiebar added a layer above the button so i could only interact with my desired element after accepting the cookies.

Since i dont know the structure of the page i can only suggest how to find such 'blocking' elements.

First i would recommend the browser dev tools and inspect every possible layout the page can have. Of course only the part you want to interact with. Check for Popup´s, cookiebars, userDialog´s so on.

After finding the issue the workaround will probably be self explainatory.

If that is not the solution it is also possible that the element did not finish loading entirely and needs some more time to complete, but since you are already waiting for 10 seconds i doubt that.

Knight
  • 205
  • 1
  • 9