Skip to content
Advertisement

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

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

JavaScript

or

JavaScript

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

JavaScript

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

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

JavaScript

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

JavaScript

Advertisement

Answer

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

JavaScript

Result

compose

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement