selenium 要素存在確認
python seleniumでtwitter投稿できない
python seleniumでtwitterのログインを行っていたところ
ログインページが2種類表示されるようなので、ログインができなかった。
表示するTwitterのログインページにIDとPASSWORD及びログインボタンのxpathが存在しないため
「elenium.common.exceptions.NoSuchElementException: Message: no such element:」エラーを吐いて停止していた。
修正前のselenium twitterログインソース
上記の画面のときは下記のコードではログインできない。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
driver = webdriver.Chrome('C:/Users/user/PycharmProjects/pypy3/chromedriver.exe') driver.maximize_window() driver.get('https://twitter.com/login') WAIT_SECOND=8 #driver.find_element_by_xpath('//*[@id="doc"]/div/div[1]/div[1]/div[2]/div[2]/div/a[2]').click() time.sleep(3) id = driver.find_element_by_xpath('//*[@id="page-container"]/div/div[1]/form/fieldset/div[1]/input') id.send_keys(login_id) password = driver.find_element_by_xpath('//*[@id="page-container"]/div/div[1]/form/fieldset/div[2]/input') password.send_keys(pas) driver.find_element_by_xpath('//*[@id="page-container"]/div/div[1]/form/div[2]/button').click() |
tryを追加して要素無しを避ける方法
改めて、停止したログインページからxpathを取得して要素の存在確認を行う代わりに例外処理を追加し対応した
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
try: id = driver.find_element_by_xpath('//*[@id="page-container"]/div/div[1]/form/fieldset/div[1]/input') id.send_keys(login_id) password = driver.find_element_by_xpath('//*[@id="page-container"]/div/div[1]/form/fieldset/div[2]/input') password.send_keys(pas) driver.find_element_by_xpath('//*[@id="page-container"]/div/div[1]/form/div[2]/button').click() except: id = driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[1]/main/div/div/form/div/div[1]/label/div[2]/div/input') id.send_keys(login_id) password = driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[1]/main/div/div/form/div/div[2]/label/div[2]/div/input') password.send_keys(pas) driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[1]/main/div/div/form/div/div[3]/div').click() |
投稿用の全ソース
なおツイートするまでのサンプルコードは下記コードです。
ローカルフォルダに指定の画像が存在する場合は、画像付き投稿。
画像が存在しない場合は、文章のみ投稿することにしました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# -*- coding: utf-8 -*- import re import time from time import sleep import os import sys from selenium import webdriver 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.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys import urllib.parse login_id="user id" pas="passwrod" driver = webdriver.Chrome('C:/Users/user/PycharmProjects/pypy3/chromedriver.exe') driver.maximize_window() driver.get('https://twitter.com/login') time.sleep(3) try: id = driver.find_element_by_xpath('//*[@id="page-container"]/div/div[1]/form/fieldset/div[1]/input') id.send_keys(login_id) password = driver.find_element_by_xpath('//*[@id="page-container"]/div/div[1]/form/fieldset/div[2]/input') password.send_keys(pas) driver.find_element_by_xpath('//*[@id="page-container"]/div/div[1]/form/div[2]/button').click() except: id = driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[1]/main/div/div/form/div/div[1]/label/div[2]/div/input') id.send_keys(login_id) password = driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[1]/main/div/div/form/div/div[2]/label/div[2]/div/input') password.send_keys(pas) driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[1]/main/div/div/form/div/div[3]/div').click() #ここまでログイン↑ #ここからTweet↓ path="D:\xxx.jpg" tweet ='投稿したい文章' if os.path.exists(path): # 画像があるとき try: time.sleep(4) elem = driver.get('https://twitter.com/compose/tweet') driver.set_window_size(531,828) time.sleep(2) driver.find_element_by_xpath("//input[@type='file']").send_keys(path) time.sleep(2) driver.find_element_by_css_selector('.notranslate.public-DraftEditor-content').send_keys(tweet) time.sleep(2) driver.find_element_by_xpath(u"(.//*[normalize-space(text()) and normalize-space(.)='キーボードショートカットを表示'])[1]/following::span[3]").click() print("input_done") except KeyboardInterrupt: print("no twee.\n") sys.exit() else: #ローカルフォルダに画像がないときは、文章のみ投稿する。 try: time.sleep(4) elem = driver.get('https://twitter.com/compose/tweet') time.sleep(2) driver.find_element_by_css_selector('.notranslate.public-DraftEditor-content').send_keys(tweet) time.sleep(2) driver.find_element_by_css_selector('div.css-18t94o4.css-1dbjc4n.r-urgr8i.r-42olwf.r-sdzlij.r-1phboty.r-rs99b7.r- 1w2pmg.r-1n0xq6e.r-1vuscfd.r-1dhvaqw.r-1fneopy.r-o7ynqc.r-6416eg.r-lrvibr').click() print("input_done") except KeyboardInterrupt: print("no tweet.\n") sys.exit() sys.exit() |
備考: ログインページが2種類表示される仕様についてはよくわかりませんでした。誰か教えてほしい
最近のコメント