【发布时间】:2021-05-16 18:49:38
【问题描述】:
我刚刚开始第一次在 Python 中使用 selenium,在学习了一个快速教程之后,我现在正在尝试使用它制作一个程序,该程序将登录到 Gmail,然后将电子邮件发送到选定的电子邮件地址。
我已经完成了登录部分,但在撰写新的电子邮件部分时遇到了一些问题(仅在某些时候有效),而且每次写邮件正文时都会卡住。
我的代码在下面,我尝试阅读文档,但我无法在 Gmail 中使用以下内容,当我检查 gmail 中的元素时,它似乎比示例中的基本 html 结构复杂得多:
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('My_email@gmail.com')
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('my_other_email@gmail.com')
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()
【问题讨论】:
-
什么是 find_element_by_????
-
我放在那里的问号表示我不清楚该做什么,我已经厌倦了通过类名、标签、标题文本和其他东西找到它们,但它们都没有用....