python获取未读邮箱数目_Python打开每个未读邮件

你能建议一下如何处理每封未读邮件的打开过程吗?在

现在它是这样工作的:登录Gmail

打开收件箱

点击未读邮件

返回收件箱

打开下一封未读邮件

但我注意到,selenium并没有打开每封邮件。硒通过一封邮件打开。我试图使用index-=1,但它不起作用。在

有代码:from selenium import webdriver

from selenium.webdriver.common.keys import Keys

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.by import By

from selenium.common.exceptions import TimeoutException

from selenium.webdriver.support.color import Color

driver = webdriver.Chrome(executable_path=r'/chromedriver_win32/chromedriver.exe')

driver.implicitly_wait(10)

driver.get('https://gmail.com')

assert 'Gmail' in driver.title

try:

login = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//*[@id='identifierId']")))

finally:

driver.find_element_by_xpath("//*[@id='identifierId']").send_keys("********@gmail.com" + Keys.RETURN)

try:

password = WebDriverWait(driver, 10).until(

EC.presence_of_element_located((By.XPATH, "//*[@id='password']/div[1]/div/div[1]/input")))

finally:

driver.find_element_by_xpath("//*[@id='password']/div[1]/div/div[1]/input").send_keys(

"PASSWORD" + Keys.RETURN)

wait = WebDriverWait(driver, 30)

unread_mails = driver.find_elements_by_xpath("//*[@class='zF']")

for index, mails in enumerate(unread_mails):

print(unread_mails[index])

if unread_mails[index].is_displayed():

wait.until_not(EC.staleness_of(unread_mails[index]))

try:

unread_mails[index].click()

index -= 2

except StaleElementReferenceException:

pass

driver.get('https://mail.google.com')

try:

WebDriverWait(driver, 5).until(EC.alert_is_present(),

'Timed out waiting for PA creation ' +

'confirmation popup to appear.')

confirmation_alert = driver.switch_to.alert

confirmation_alert.accept()

except TimeoutException:

print('no alerts')

unread_mails = driver.find_elements_by_xpath("//*[@class='zF']")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值