0

I have just started using selenium with Python for the first time, after following a quick tutorial I am now trying to make a program with it that will login to Gmail and then send an email to a chosen email address.

I've gotten the login part done but had some problems with the composing a new email part (only works some of the time) and I get stuck everytime when it comes to writing the message body.

My code is below, I have tried reading the docs but Im having trouble getting the following to work in Gmail and the when I inspect the elements in gmail it seems a lot more complex than the basic html structures in the examples here:

http://selenium-python.readthedocs.org/locating-elements.html#locating-elements-by-tag-name

"""
Write a program that takes an email address and string of text on the command line and then, using Selenium,
logs into your email account and sends an email of the string to the provided address. 
"""

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Chrome()
browser.get('http://www.gmail.com')

emailElem = browser.find_element_by_id('Email')
emailElem.send_keys('[email protected]')
emailElem.submit()

time.sleep(2)

passwordElem = browser.find_element_by_id('Passwd')
passwordElem.send_keys('My_password_here')
passwordElem.submit()


time.sleep(2)

composeElem = browser.find_element_by_class_name('z0') #this only works half of the time
composeElem.click()

time.sleep(7)

toElem = browser.find_element_by_name("to")
toElem.send_keys('[email protected]')

time.sleep(2)

subjElem = browser.find_element_by_name("subjectbox")
subjElem.send_keys('Test with selenium')

time.sleep(2)

bodyElem = browser.find_element_by_???('???') #this is where I get stuck and not sure what to do here
bodyElem.send_keys('A test email with selenium')

time.sleep(2)

sendElem = browser.find_element_by_link_text('send') #not sure if this is correct too
sendElem.submit()
2
  • what is find_element_by_???? Commented Nov 24, 2015 at 8:45
  • the question marks I put there to indicate that is where Im unclear what to do, I had already tired find by class name, tag, title text and other things and none of them worked.... Commented Nov 24, 2015 at 9:32

4 Answers 4

1

I think the easiest way to select elements on a loaded page is to find them by css selector. You can find them in the browser inspector, and then copy their unique css selector (in firefox press inspect element -> copy unique selector). In this case this should work:

browser.find_element_by_css_selector('#\:nw')
Sign up to request clarification or add additional context in comments.

4 Comments

ok thanks, I did try something with ':nw' before but had no luck, will give this a shot now
this method didn't work Im afraid, it returns this error: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"#\:nw"}
well, it worked for me, but I launched it from interactive shell when page was fully loaded. Such exception appears, when there is not yet element on the page. So, first, try it from interactive shell, and for automated script you can use selenium waits (selenium-python.readthedocs.org/waits.html). Use explicit wait for the last find_element, or implicit to get rid of all time.sleep's.
sorry yes I wasn't running it from the shell, it does work that way thanks, will try the waits now
1

Please Try :

time.sleep(10)

bodyElem = browser.find_element_by_xpath("//*[@id=":ov"]")

OR

bodyElem = browser.find_element_by_xpath("//*[@id=":ou"]")

I assume that it need little more time to find element so I have increased sleep time and also given xpath should work.

3 Comments

neither of these worked too Im afraid, with :ov it returns this error: selenium.common.exceptions.ElementNotVisibleException: Message: element not visible and with :ou this error: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id=":ou"]"}
it seems that a lot of the elements change too making it problematic, Ive noticed that the 'z0' class name I use for composeElem only works if I fully quit Chrome each time I run this otherwise the element isn't there. For the message body Ive seen in the source 'aria-label="Message Body" ' there every time though, how would I got about telling selenium to use that element?
For me same code is working so it should work for you.
1

As id is changing each time, we can check for aria-label :

browser.find_element_by_css_selector("div[aria-label='Message Body']")

For send, use this :

sendElem = browser.find_element_by_xpath("//div[text()='Send']")
sendElem.click()

Comments

0

You can also hack it using the following once you've found the message box (I'm a noob and couldn't get Send to work)

sendElem.send_keys(Keys.TAB) sendElem.send_keys(Keys.CONTROL + Keys.RETURN)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.