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 :
解决方案
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”)