0

I'm trying to send an email from Gmail's web interface with Python using Selenium, but I'm stuck on the part of selecting the Compose part.

This is my code:

from selenium import webdriver
import time

driver = webdriver.Chrome('C:\chromedriver.exe')


driver.get('https://www.gmail.com/')


driver.implicitly_wait(5)

loginBox = driver.find_element_by_xpath('//*[@id ="identifierId"]')
loginBox.send_keys('xxxxxxxxxxxxxxxxx')

nextButton = driver.find_elements_by_xpath('//*[@id ="identifierNext"]')
nextButton[0].click()

passWordBox = driver.find_element_by_xpath('//*[@id ="password"]/div[1]/div / div[1]/input')
passWordBox.send_keys('xxxxxxxxxxxx')

nextButton = driver.find_elements_by_xpath('//*[@id ="passwordNext"]')
nextButton[0].click()

time.sleep(2)

#compose = driver.find_element_by_xpath('/html/body/div[7]/div[3]/div/div[2]/div[1]/div[1]/div[1]/div[2]/div/div/div/div[1]/div/div')                         
#compose.click()

driver.get('https://mail.google.com/mail/u/0/#inbox?compose=new')

time.sleep(2)


compose_button_xpath="/html/body/div[7]/div[3]/div/div[2]/div[1]/div[1]/div[1]/div[2]/div/div/div/div[1]/div/div"
#driver.find_element_by_xpath(compose_button_xpath).click()
driver.find_element_by_xpath("//textarea[@name='to']").send_keys("abc@gmail.com")
driver.find_element_by_xpath("//*[@id=':12s']").send_keys("Content")
driver.find_element_by_xpath("//*[@id=':11d']").click()

Can you help me? How can I solve this?

tripleee
  • 175,061
  • 34
  • 275
  • 318
Spvda
  • 23
  • 1
  • 8
  • Have you considered using e.g. smtplib instead of web automation? That would be a cleaner and simpler way of sending emails compared to using selenium. try googling 'send gmail with python smtplib' or similar – tbjorch Dec 08 '21 at 10:56
  • Yes I already considered it, but what I am trying to do is automate it by selenium. Thank you – Spvda Dec 08 '21 at 10:57
  • Ok, i don't have time to look into details now, but a guess would be that the popup with the message etc. is inside another window handle (e.g. in iframe) meaning that you would need to switch context to reach it. Try something like in this answer: https://stackoverflow.com/questions/66568508/selenium-switch-to-popup-window/66568862#66568862 – tbjorch Dec 08 '21 at 10:59
  • Does this answer your question? [How to send an email with Gmail as provider using Python?](https://stackoverflow.com/questions/10147455/how-to-send-an-email-with-gmail-as-provider-using-python) – loopassembly Dec 08 '21 at 11:07
  • No, I think I was clear and punctual in wanting to automate gmail with selenium – Spvda Dec 08 '21 at 11:08
  • Hi Spvda. Sorry, not sure what the problem exactly is... Are you having difficulty locating Recipients, Subject, the body of the email, the Send button? – Alichino Dec 08 '21 at 11:33
  • Nop. I have difficulty locating the xpath of Compose mail with selenium – Spvda Dec 08 '21 at 11:35
  • I can't find a way to locate "compose_button". Only with the url, but it is not very efficient ("https://mail.google.com/mail/u/0/#inbox?compose=new"...). Also I can't find the" body of message" button. – Spvda Dec 08 '21 at 11:37

1 Answers1

0

Wow, the HTML for the Gmail is a right mess.

Anyway, I've located the Compose button with this xPath:

'//*[contains(@class, "T-I") and contains(@class, "L3") and contains(@class, "T-I-KE")]'
Alichino
  • 1,668
  • 2
  • 16
  • 25
  • I get this: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element – Spvda Dec 08 '21 at 12:17
  • driver.find_element_by_xpath('//*[contains(@class, "T-I") and contains(@class, "L3") and contains(@class, "T-I-KE")]').click() – Spvda Dec 08 '21 at 12:18
  • I saw this video but it didn't work for me :/ https://www.youtube.com/watch?v=vkRpOIuvwkg&ab_channel=DeepDataScience , and yep, the HTML of Gmail is a right mess – Spvda Dec 08 '21 at 12:29
  • When you Inspect the button, what class(es) does it have for you? – Alichino Dec 08 '21 at 14:20
  • Could be they're dynamic :( – Alichino Dec 08 '21 at 14:20