'how can I click on element based on a condition from a list of elements in python selenium?

this image is where I am trying to upload the photos. In the image there is image holder in which it keeps loading and other images are loaded fine. I want to be able to remove the image that keeps loading by clicking on the cross above it.

Hi guys, before I start I just want to say I am very very new at python so apologies if I sound nooby. So I am trying to automate uploading images to a website. However, all is going well but I see that there is some error and some image keeps randomly loading after being uploaded.

I want to close the image if they do not load within 30 seconds. So far from my code I am able to identify the holders that have not loaded. I am trying to click the cross element to remove the image that is not loading. But I cannot seem to get my head around how I can do this. With the current code it is removing the first few image but not the one what I am trying to remove which is the one which keeps loading. I tried couple of ways but all failed. Can anyone help me out with this. I have been stuck on this for a few days now.

The code I am sharing is only a snippet from the full script. I feel that there is no need to share the whole code so I am just sharing the the corresponding code.

Here it is:

    loading_images = driver.find_elements_by_xpath('//*[contains(@class, "lzcic4wl j83agx'
                                                   '80 btwxx1t3") and contains(@role, "progressbar")]')

    delete_cross = driver.find_element_by_xpath(
        '//*[contains(@aria-label, "Remove") and contains(@class, "oajrlxb2 qu0x0")]')
        
    time.sleep(30)

    if loading_images:
        for i in loading_images:
            delete_cross.click()
            print('loading image remove')
    else:
        print('no loading images')


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source