I am trying to send an image by WhatsApp with Python and Selenium.
Code:
driver_path = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(driver_path, chrome_options = options)
driver.get('https://web.whatsapp.com/')
contacto = "1100001111" #telefono
mgs = "Prueba desde WhatBot"
#ruta de imagen
imagen = "C:\pt-Python\scrap\WhatBot\banner.png"
WebDriverWait(driver, 5)\
.until(EC.element_to_be_clickable((By.XPATH,"//*[@id='main']/footer/div[1]/div[2]/div/div[2]")))\
.send_keys(imagen)
The process does it well, it enters WhatsApp, searches for the user through their phone number and sends the message, but when I want to send an image I don't get this error.
#Mistake
image = "C:\pt-Python\scrap\WhatBot\banner.png" ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated \xXX escape [Finished in 0.2s]
I appreciate the help in advance.
When used
\
in a string, Python interprets it as an escape character if it is followed by certain letters. To avoid this, you can write\\
. More information .Specifically, the error is that in the address appears
\b
, which is the escape character corresponding to the backspace.Solutions :
Recommended option:
O well:
A little shorter:
To send you the image: