Unable to add multiple email addresses to sendkeys() while using Selenium - python

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!

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)

UPDATE: SOLUTION FOUND
The email addresses require space in between and it works just fine without looping.

Related

FInding the latest message in a list of messages

so im currently trying to copy a message to automate a login sequence. The login requires an otp code that has to be sent to a phone. I set up a textnow account to gain access to the otp via web page but the problem comes in when trying to find the latest message. How could I specify that I want the latest element copied even though the html for each individual message looks exactly the same. The only thing that differentiates them is the otp given and time.
How could I identify the latest message to the bot and copy it ?
senderofotp = driver.find_element_by_xpath('//*[#id="chat-preview-list"] /ul/li[1]/div[2]/div[1]/span/div')
senderofotp.click()
The code above is how I selected the sender of the message.
message = driver.find_element_by_xpath('//*[#id="messageView"]/ul/li[30]/div/div[1]/div/div[1]/div/span/span')
even when I try to grab the message it gives me the xpath to the message view as a whole
you may use find_elements_by_xpath to find list of elements at once

Need help understanding looping a program/ nested loops in Python

Working on a program to automate some daily functions that I do using Selenium to interact with a browser.
I have the script working exactly as I want it to with one email account, however I have several I would like to automate. I would like it to be able to choose one email, complete the entirety of the code, loop back to the beginning, and continue from the beginning, but with my next account in the variable.
For example, my login data is stored like this:
emails=('email1#email.com',
'email2#email.com',
'email3#email.com',
)
myPassword=('passwordtext')
And is called during the login process in this way, just for reference:
for email in emails:
emailid=driver.find_element_by_name('email')
emailid.send_keys('email') # this is the variable that would need to change each time
password=driver.find_element_by_name('password')
password.click()
password.send_keys(myPassword)
password.send_keys(Keys.ENTER)
# then, several more work functions are completed using Selenium
Obviously, this doesn't work and leads to all of the email addresses in the variable being cycled through, and then the code continues. What I would like is for one email address to be selected, the rest of the program is executed, and then it loops back to the beginning and continues with the next email address in the variable.
I'm pretty sure the solution is a nested loop here and that this is a pretty basic question, but I can't get my head around how to set that up. Again, the rest of the program works perfectly, I'm just trying to figure out a way to loop through the program, changing only the login information, until the end of the variable. I hope this makes sense.
Everything that you want to run for each email address needs to be indented under your for email in emails: line.
There is no need for a nested loop.
for email in emails:
stuff_that_runs_once_per_email_address
stuff_that_runs_independent_of_email_address

Selenium types email password incorrectly

I'm writing a little Python program to automate the e-mail authentication process using selenium. Now, when I execute the program I always get the "email or password incorrect" message even if I'm sure that they are correct; I tried to execute the program line by line in the Python shell and I also changed the password to one without special characters just to be sure that selenium wasn't having problem with these specifically but without success so I think that somehow selenium is not "typing" the password correctly in the authentication form.
I'm using python 3.6; here's the code (without the real username and password for security reasons, of course).
from selenium import webdriver
browser = webdriver.Safari()
browser.get("myEmailProvider")
emailElem = browser.find_element_by_id("usernameDisplay")
emailElem.send_keys("myEmail")
passwordElem = browser.find_element_by_id("password")
passwordElem.send_keys("myEmailPassword")
passwordElem.submit()
Does anyone have any idea about why this is happening?
Thanks.
I had similar problem when I used Selenium to enter username and password. I have noticed that It would sometimes "eat" letter of two. For example, instead of typing "password" it would type "passord" or something like that.
I tried to find why this is happening, but without luck, however, I found workaround for this.
I put these things into while loop. So in your example I would do:
passwordElem = browser.find_element_by_id("password")
while(passwordElem != "myEmailPassword")
passwordElem.send_keys("myEmailPassword")
passwordElem.submit()code here
You can also put some limit how much times it can loop before It throws error, so you don't end up in infinite loop, but from my experience it send correct values in max 3 tries.
To work around this the best approach is to fill the form data in via javascript:
driver.execute_script('document.getElementById("usernameDisplay").value="myEmail"')

Making a Python program handle mailto links

I made a Python program that is capable of sending email. However, I want to make it capable of being a default email client such that it will capture the email address and subject of HTML email links (mailto links) when the user clicks them.
How do I get the email address and subject to my client?
Currently, I can set my program as the default mail client, but I don't know what information, or format of information, it's getting from the web browser; so, I'm not sure how to parse it.
Assuming the complete link is passed in as sys.argv[1], you need to do something like this:
import urllib.parse
parsed = urllib.parse.urlparse(sys.argv[1])
mail_addr = parsed.path
fields = urllib.parse.parse_qs(parsed.query)
This sets mail_addr to the address to send the email to, while field will be a dictionary of additional parameters.
The fields that you can expect to be present are specified in RFC 6068

Best Python Library for Downloading and Extracting Addresses

I've just been given a project which involves the following steps
Grab an email from a POP3 address
Open an attachment from the email
Extract the To: email address
Add this to a global suppression list
I'd like to try and do this in Python even though I could it in PHP in half the time (this is because I dont know anywhere near as much Python as PHP)
My question would be.
Can anyone recommend a Python library for interacting with email in the way described above?
Many thanks in advance
Two bits from the standard library: poplib to grab the email via POP3, email to slice and dice it as you wish.

Categories