Python w/ Selenium Gmail Email Send Automating To: Field is Giving Me Trouble

Tags: , , ,

The end goal is to send myself an email if my public ip address changes, as I don’t have dynamic dns and have to manually enter the ip addresses myself for my web server. I’ve done all I possibly can to try and get bash utilities to do the job for me, but CenturyLink is unfortunately out to block me no matter how I configure my outbound mail.

So I’ve turned to graphical python/selenium web page automation, which will sign into my gmail account for me, click the ‘compose’ button, then enter in the To:, Subject:, and text segments and hit send. Everything is working except for one small part – the To: field. The html/css is different for this than all the others and no matter how I try to select the field using




I just can’t seem to fill out the field. Bash will give me an error like

:lo cannot be reached by keyboard 

or textarea#:lo.vO is not a valid selector

When I did an inspect element, the element looked like this:

<textarea rows="1" id=":lo" class="vO" name="to" spellcheck="false" autocomplete="false" autocapitalize="off" autocorrect="off" tabindex="1" dir="ltr" aria-label="To" role="combobox" aria-autocomplete="list" style="width: 462px;"></textarea>

My code so far is this: (note: which does not include getting ip info yet, just gmail login / manipulation)

from selenium import webdriver
import time

driver = webdriver.Firefox();
username = driver.find_element_by_id('identifierId');
time.sleep(2); #password not entered in username field
password = driver.find_element_by_class_name('whsOnd.zHQkBf');

#end login, start composing

time.sleep(5); #wait for sign in
to = driver.find_element_by_class_name('textarea#:lo.vO'); #incorrect
subject = driver.find_element_by_id(':l6');
subject.send_keys("IP Address changed");
content = driver.find_element_by_id(':m9');
content.send_keys("Test Testn");


I think there seems to be a dynamic variation with the element ids in different browser. For me when I tried to compose the mail to fetch the XPATH I noted the XPATH was //*[@id=":oa"] but while the script launched it was //*[@id=":my"].

To accommodate this I have used element querying using XPATH //textarea[1] as the Recipients section is always the first textarea. This proves to work well consistently across different browser sessions.

Code Snippet

>>> d = webdriver.Chrome()
[14424:7728:0809/] Unable to read registry value HKLMSOFTWAREPoliciesGoogleChromeMachineLevelUserCloudPolicyEnrollmentToken for writing result=2

DevTools listening on ws://
>>> d.get('')
>>> d.find_element_by_xpath(xpath)
<selenium.webdriver.remote.webelement.WebElement (session="6072286733856e53b69af89ea981001c", element="0.42218760484088036-1")>
>>> d.find_element_by_xpath('//textarea[1]').send_keys('')



Source: stackoverflow