Selenium python duplicate images

自作多情 提交于 2020-02-02 12:49:45

问题


Using python to upload images to control over selenium. It works perfect but for some reason it duplicate the images.

I.E - First image is uploaded Upload as main picture is confirmed. Second image is uploaded Third images is uploaded + 2 image re uploaded etc ...

Using this code

for pair in pair_list:
    file = pathlib.Path(pair)
    if file.exists ():
        #HERE I RESIZE PHOTO
        basewidth = 580
        img = Image.open(pair)
        wpercent = (basewidth/float(img.size[0]))
        hsize = int((float(img.size[1])*float(wpercent)))
        img = img.resize((basewidth,hsize), Image.ANTIALIAS)
        newName = pair.replace('.jpg','_resized.jpg')
        img.save(newName, quality = 95) 
        pair = newName
        myImagesToDelete.append(pair)
        #END OF RESIZE
        #print('Uploading photo:' + pair)

        if firstpic == True:
            firstpic = False
            #ADD MAIN PICTURE
            try:
                myElem = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, 'img-file-input')))    
                myElem.send_keys(pair)
            except TimeoutException:
                errorDuringFill = True
            time.sleep(5)
            print('First picture added ' + pair)
            #CONFIRM PICTURE
            try:
                myElem = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/main/div[2]/div/div/div[2]/section[1]/div/div[2]/div[2]/div[2]/div[3]/div/button[2]')))
                myElem.click()
            except TimeoutException:
                errorDuringFill = True
            print('First picture confirmed')
        else:
            #ADD OTHER PICTURES
            print('Adding it as other picture: ' + pair)
            try:
                myElem = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, 'img-file-input')))  
                myElem.send_keys(pair)
            except TimeoutException:
                errorDuringFill = True
            #time.sleep(2)

Paths is good which i can see in log Loops are good. in total of 5 loops i have 8 images added.

Python log log as it shows: Python log

Screen after upload : Screen

GIF :


回答1:


For removing duplicate images, you can upload all images at one time so all images will be uploaded as duplicate.

myElem.send_keys(“imagePath1” + \n + “imagePath2”)


来源:https://stackoverflow.com/questions/59955049/selenium-python-duplicate-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!