2017-06-07 20 views
0

未読メールの開封方法を教えてください。Pythonで未読メールを開く

今ではこのように動作します:Gmailの

  • オープンの受信トレイ
  • 戻る受信トレイ
  • オープンに未読メール
  • をクリックし、次の未読メール
    1. ログイン

      しかし、私はセレンが各メールを開くわけではないことに注意しました。セレンは1通のメールで開きます。私はインデックス= 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']") 
      
    +1

    xpath // * [@ class = 'zF']によって返される要素の数を確認してください。それは1つだけかもしれません。また、Gmailには動的なクラス名があります。だからあなたはそれを使って未読メールを見つけることはできません。 – Murthi

    +0

    ありがとうございます。私はそれが良いように見える要素の数を確認しました。それは動的なクラス名ではないようです。セレンはすべての要素を通過しますが、各メールは通過しません。毎回1つのメールをスキップします。 – Jack

    +1

    gmail APIを使用しないだけの理由はありますか?それはもっと簡単だろう。 – JeffC

    答えて

    1

    セレンを使用するか、この問題を解決するために廃棄いくつかの理由のために最良の選択ではありません。

    コードがあります。

    1. Googleには動的なHTML読み込みがあり、多くの問題が発生する可能性があります。
    2. あなたはreCaptchaサービス(ロボットではありません)に出くわしたら何がありますか?
    3. この違法なGoogleの操作はブロックされている可能性があります。

    この問題を解決するには、Google APIを使用することをおすすめします。

    関連する問題