您能否建议如何处理每封未读邮件的开放流程?
现在它的工作原理如下:
但我注意到硒没有打开每封邮件。硒通过一封邮件打开。我尝试使用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 :(得分:1)
出于某些原因,使用硒或报废来解决这个问题不是最好的选择。
因此,我真的建议您使用Google API来解决此问题。