I'm trying to automate the sending of messages with an attachment attached to a series of numbers that I insert from the program via an external txt. The program works well, the numbers are read correctly, the message is inserted into the box and also sent to everyone correctly. the only problem is that the image doesn't insert and I don't understand why
f = open("message.txt", "r", encoding="utf8")
message = f.read()
f.close()
print(style.YELLOW + '\nQuesto è il tuo messaggio-')
print(style.GREEN + message)
message = quote(message)
files = []
f_num = 0
f = open("files.txt", "r")
for media in f.read().splitlines():
if media.strip() != "":
files.append(media.strip().replace("\\", "/"))
f_num += 1
print(style.YELLOW + 'Found ' + str(f_num) + ' File(s)' + style.RESET)
numbers = []
f = open("numbers.txt", "r")
for line in f.read().splitlines():
if line.strip() != "":
numbers.append(line.strip())
f.close()
total_number=len(numbers)
print(style.RED + 'Ho trovato ' + str(total_number) + ' numeri nel file' + style.RESET)
delay = 30
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
print('Quando ti si apre il browser, scannerizza il codice e accedi (non dovrebbe chiederlo in futuro)')
driver.get('https://web.whatsapp.com')
input(style.MAGENTA + "Dopo aver completato il login e le tue chat sono visibili, premi ENTER" + style.RESET)
for idx, number in enumerate(numbers):
number = number.strip()
if number == "":
continue
print(style.YELLOW + '{}/{} => Sto inviando un messaggio a {}.'.format((idx+1), total_number, number) + style.RESET)
try:
url = 'https://web.whatsapp.com/send?phone=' + number + '&text=' + message
sent = False
for i in range(3):
if not sent:
sleep(3)
driver.get(url)
try:
click_btn = WebDriverWait(driver, delay).until(EC.element_to_be_clickable((By.XPATH, "//span[@data-icon='send']")))
except Exception as e:
pass
else:
sleep(1)
click_btn.click()
sleep(0.5)
if f_num != 0:
try:
for file in files:
# Muovi la definizione di attach_btn dentro il loop for
attach_btn = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH, "//span[@data-testid='attach-menu-plus']")))
# Inserisci qui la sezione del codice per l'allegato
attach_btn.click()
sleep(1)
file_input = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH, "//input[@accept='image/*,video/mp4,video/3gpp,video/quicktime']")))
file_input.send_keys(file)
sleep(1)
driver.find_element(By.XPATH, "//span[@data-icon='send']").click()
print(style.GREEN + 'File allegato {} a {}'.format(file, number) + style.RESET)
except Exception as e:
print(style.RED + 'Errore nell\'allegare il file {} a {}: {}'.format(file, number, str(e)) + style.RESET)
sent=True
sleep(3)
print(style.GREEN + 'Ho inviato il messaggio a: ' + number + style.RESET)
except Exception as e:
print(style.RED + 'Errore, non sono riuscito a inviare il messaggio, scusami ;( ' + number + str(e) + style.RESET)
print(style.MAGENTA + 'Ho finito di inviare i messaggi, grazie per avermi usato xD' + style.RESET)
driver.close()
I tried to solve the problem, look for a solution also using chat gpt but nothing