0

I am trying to implement a tool that checks whether a website has a drag and drop file upload functionality. Specifically, if a website such as this has a drag and drop file upload functionality, my tool needs to return True.

I found this code this Python code which utilizes Selenium to upload a file from the local file system to a website with drag and drop. It needs an element_id of the drag/drop area and the path of the file.

I updated this code to build my tool. Specifically, my code first retrieves all of the elements of the website and checks whether the file is droppable returns True. However, it still returns True even though the some 'ids' given to the functions are not droppable. I want to get return True only if the given "id" uploads to the file to the webserver.

ids = driver.find_elements_by_xpath('//*[@id]')
for ii in ids:
    all_ids.append(ii.get_attribute('id'))
    #print(ii.get_attribute('id'))    # id name as string
    try:
        dropzone = driver.find_element_by_id(ii.get_attribute('id'))
        dropzone.drop_files("/Users/pythonSelenium/test.png")
        return True

    except: 
        continue

How can I fix that issue?

kne1234
  • 13
  • 2

1 Answers1

0

You could use a boolean value which is set to true only if the element is dropped, otherwise returning false.

ids = driver.find_elements_by_xpath('//*[@id]')
for ii in ids:
    all_ids.append(ii.get_attribute('id'))
    #print(ii.get_attribute('id'))    # id name as string
    dropped = False
    try:
        dropzone = driver.find_element_by_id(ii.get_attribute('id'))
        dropzone.drop_files("/Users/pythonSelenium/test.png")
        dropped = True
    except: 
        continue
return dropped
randomer64
  • 70
  • 6