简体   繁体   English

Python w/Selenium Gmail 电子邮件自动发送至:Field 给我带来了麻烦

[英]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.最终目标是在我的公共 ip 地址发生变化时给自己发送一封电子邮件,因为我没有动态 dns 并且必须自己为我的 Web 服务器手动输入 ip 地址。 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.我已经尽我所能尝试让 bash 实用程序为我完成这项工作,但不幸的是,无论我如何配置出站邮件,CenturyLink 都会阻止我。

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.所以我转向了图形化的 python/selenium 网页自动化,它将为我登录我的 gmail 帐户,单击“撰写”按钮,然后输入收件人:、主题:和文本段并点击发送。 Everything is working except for one small part - the To: field.除了一小部分 - To: 字段之外,一切都在工作。 The html/css is different for this than all the others and no matter how I try to select the field using html/css 与其他所有不同,无论我如何尝试使用

driver.find_element_by_class_name()

or或者

driver.find_element_by_id()

I just can't seem to fill out the field.我似乎无法填写该字段。 Bash will give me an error like Bash会给我一个错误,比如

:lo cannot be reached by keyboard 

or textarea#:lo.vO is not a valid selector或 textarea#:lo.vO 不是有效的选择器

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)到目前为止,我的代码是这样的:(注意:还不包括获取 ip 信息,只是 gmail 登录/操作)

from selenium import webdriver
import time

driver = webdriver.Firefox();
driver.get('https://www.gmail.com');
username = driver.find_element_by_id('identifierId');
username.send_keys("EMAIL");
driver.find_elements_by_class_name('RveJvd.snByac')[1].click();
time.sleep(2); #password not entered in username field
password = driver.find_element_by_class_name('whsOnd.zHQkBf');
password.send_keys("PASSWORD");
driver.find_elements_by_class_name('RveJvd.snByac')[0].click();

#end login, start composing

time.sleep(5); #wait for sign in
driver.find_element_by_class_name('T-I.J-J5-Ji.T-I-KE.L3').click();
to = driver.find_element_by_class_name('textarea#:lo.vO'); #incorrect
to.send_keys("EMAIL");
subject = driver.find_element_by_id(':l6');
subject.send_keys("IP Address changed");
content = driver.find_element_by_id(':m9');
content.send_keys("Test Test\n");

Have you tried to use the Gmail API?您是否尝试过使用 Gmail API? It's easier faster and more efficient than using Selenium.与使用 Selenium 相比,它更容易、更高效。

Here's the quickstart: https://developers.google.com/gmail/api/quickstart/python这是快速入门: https : //developers.google.com/gmail/api/quickstart/python

(I'm writing an answer because I don't have the reputation to just comment) (我正在写一个答案,因为我没有评论的声誉)

You can also use Python's built-in email package:您还可以使用 Python 的内置电子邮件包:

https://docs.python.org/3/library/email.examples.html https://docs.python.org/3/library/email.examples.html

I think there seems to be a dynamic variation with the element ids in different browser.我认为不同浏览器中的元素 ID 似乎存在动态变化。 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"] .对我来说,当我尝试撰写邮件以获取 XPATH 时,我注意到 XPATH 是//*[@id=":oa"]但是当脚本启动时它是//*[@id=":my"]

To accommodate this I have used element querying using XPATH //textarea[1] as the Recipients section is always the first textarea .为了适应这一点,我使用 XPATH //textarea[1]元素查询,因为 Recipients 部分始终是第一个textarea This proves to work well consistently across different browser sessions.这证明可以在不同的浏览器会话中始终如一地工作。

Code Snippet代码片段

>>> d = webdriver.Chrome()
[14424:7728:0809/135301.805:ERROR:install_util.cc(597)] Unable to read registry value HKLM\SOFTWARE\Policies\Google\Chrome\MachineLevelUserCloudPolicyEnrollmentToken for writing result=2

DevTools listening on ws://127.0.0.1:12582/devtools/browser/31a5ab42-a4d2-46f3-95c6-a0c9ddc129d7
>>> d.get('https://www.gmail.com')
>>> 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('cswadhikar@gmail.com')

Result结果

撰写

Try this code to send an email with Gmail.尝试使用此代码使用 Gmail 发送电子邮件。 It has To, Subject and Send button functionality :它具有收件人、主题和发送按钮功能:

driver.find_element(By.XPATH, '//*[@id=":k2"]/div/div').click()# Compose button time.sleep(5) driver.find_element(By.XPATH, '//*[@id=":k2"]/div/div').click()# Compose button time.sleep(5)

driver.find_element(By.NAME, 'to').send_keys("Enter the email address of recipients")# to field in compose time.sleep(2) driver.find_element(By.NAME, 'to').send_keys("请输入收件人的电子邮件地址")# 撰写 time.sleep(2)

driver.find_element(By.NAME,'subjectbox').send_keys("This email is send using selenium")# Subject field in compose time.sleep(2) driver.find_element(By.NAME,'subjectbox').send_keys("This email is sent using selenium")# compose time.sleep(2) 中的主题字段

driver.find_element(By.XPATH,'//*[@id=":p3"]').click()# click on send button driver.find_element(By.XPATH,'///*[@id=":p3"]').click()#点击发送按钮

time.sleep(5)时间.sleep(5)

driver.close()驱动程序关闭()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM