Search code examples
pythonuser-interfaceselenium-webdriverxpathautomated-tests

Invalid Element State Exception - import a file


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.


Solution

  • Edit: The correct term is "modal window"

    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.