Home > Back-end >  Selenium Try Catch Python [Drag and Drop File Upload]
Selenium Try Catch Python [Drag and Drop File Upload]

Time:10-26

I am trying to write a code that checks wheather a website has a drag and drop functionality. To achieve that, I am first retrieving the all elements of the website and try to upload a file with a drag and drop. However when I get the invalid id for an drag and drop I am getting a selenium.common.exceptions.WebDriverException: Message: <unknown>: Element not interactablet exception. I tried to solve it with try/catch block but it still did not work.

   ids = driver.find_elements_by_xpath('//*[@id]')
    
    counter = 0 
    for ii in ids:
        print(ii,ii.get_attribute('id'))
        driver.find_element_by_id(ii.get_attribute('id'))
        dropzone = driver.find_element_by_id(ii.get_attribute('id'))
        try:
            dropzone.drop_files("/temp/pythonSelenium/test.txt")
    
        except ElementNotInteractableException:
            continue

CodePudding user response:

You are probably catching the wrong error. The error message says:

selenium.common.exceptions.WebDriverException: Message: <unknown>: Element not interactable

As you can see, the error message source is WebDriverException and not ElementNotInteractableException. It is a bit confusing, but you have to look for the source if you want to catch an error.

So in order to catch the error you have to change your code to this:

try:
    dropzone.drop_files("/temp/pythonSelenium/test.txt")

except WebDriverException:
    continue
  • Related