你能建议一下如何处理每封未读邮件的打开过程吗?在
现在它是这样工作的:登录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']")