0

Unable to add multiple email addresses to sendkeys() while using Selenium. What I'm trying to do is send an email to multiple addreses using selenium remote webdriver to build some test cases.

The below logic only sends the email to the first recipient.

email = "xyz@gmail.com,abc@gmail.com"
driver.find_element_by_name("to").send_keys(email)

The below logic executes fine without throwing any exception but it does not generate the email at all.

emails = ["xyz@gmail.com","abc@gmail.com"]
for email in emails:
    time.sleep(5) #to wait for the element to be interactable
    driver.find_element_by_name("to").send_keys(email)

Could someone please guide in the right direction? Thanks!

  • You have an typo in your code . `driver.find_element_by_name("to").send_keys(emails)` it should be `email` not `emails` .Please rectify it – Karthik Sep 10 '20 at 19:53
  • @Karthik There was a mistake in copying the code here. I have "email" in my code. It does not work. – user14255754 Sep 10 '20 at 20:21
  • I would advice you turn off headless mode so you can actually see whats going on. (you probably just need to insert either a wait or space key somewhere, it will be clear once you see what is actually happening) – robert Sep 10 '20 at 20:50
  • @robert Yep, that's exactly what I'm doing right now! Thanks for the tip anyway! – user14255754 Sep 10 '20 at 21:09

2 Answers2

0

It’s convenient to use loop if you want to add multiple emails but i am not sure what exactly you are trying to do here, but simple solution could be

emails = "xyz@gmail.com,abc@gmail.com"
#split funtion will convert string into list split wrt “,”
emails =emails.split(',')
for email in emails:
   driver.find_element_by_name("to").send_keys(email)
Karthik
  • 2,181
  • 4
  • 10
  • 28
Assad Ali
  • 288
  • 1
  • 12
  • Thanks for this. What I'm trying to do is send an email to multiple addreses using selenium remote webdriver to build some test cases. I tried this earlier and it did not work. The script executes fine but it does not generate an email. It only sends an email if there is a single recipient for some reason. Even if I pass it as a single string with commas, it sends an email to the first recipient only. – user14255754 Sep 10 '20 at 18:02
  • Have you tried using loop? if so can you share complete script – Assad Ali Sep 10 '20 at 18:06
  • Yep, tried looping but that doesn't generate an email at all. It works perfectly fine with one recipient though so I know there is no issue with the script otherwise. I'm just struggling to add multiple addresses to the "to" & "cc" fields. Not sure if there is an easier way to share files on Stack Overflow? – user14255754 Sep 10 '20 at 18:29
  • Added all the relevant logic. Thanks! – user14255754 Sep 10 '20 at 18:53
0

UPDATE: SOLUTION FOUND

The email addresses require space in between and it works just fine without looping.