8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

selenium から Twitter にログインして投稿

Last updated at Posted at 2022-01-02

TwitterにPythonからログインして投稿する方法のメモ。
AlmaLinux, Chrom で確認。

Python 3.6.8
Google Chrome 98.0.4758.102, 96.0.4664.110
selenium 3.141.0

起動

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import chromedriver_binary
import time

# seleniumを起動
options=Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver=webdriver.Chrome(chrome_options = options)

Twitterログイン

HTMLがよく変わるから注意。
アカウントとパスワードの入力画面が別れたため2画面の処理。
各所に2秒のスリープを入れる。


def login_twitter(account, password):
    # ログインページを開く
    driver.get('https://twitter.com/login/')
    time.sleep(2)

    # account入力
    element_account = driver.find_element_by_name("text")
    element_account.send_keys(account)
    time.sleep(2) 
    # 次へボタンのXPathがこれでしか取れなかった・・・
    # 次へボタンクリック
    element_login = driver.find_element_by_xpath('/html/body/div/div/div/div[1]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div[6]')
    element_login.click()
    time.sleep(2) 

    # パスワード入力
    element_pass = driver.find_element_by_name("password")
    element_pass.send_keys(password)
    time.sleep(2) 
    # ログインボタンクリック
    element_login = driver.find_element_by_xpath('//*[@data-testid="LoginForm_Login_Button"]')
    element_login.click()
    time.sleep(2) 

各画面のスクリーンショットを撮ってデバッグしたい場合は、

driver.save_screenshot('保存する画像名')

のコードで実装可能

投稿

def send_tweet(text):
    # テキスト入力
    element_text = driver.find_element_by_class_name("notranslate")
    element_text.click()
    element_text.send_keys(text)
    # ツイートボタン
    tweet_button = driver.find_element_by_xpath('//*[@data-testid="tweetButtonInline"]')
    driver.execute_script("arguments[0].click();", tweet_button)

click()は失敗することもあるので、execute_scriptメソッドで実行

サンプル

seleniumのバージョンアップ対応

例)

def login_twitter(account, password):
    # ログインページを開く
    driver.get('https://twitter.com/login/')
    time.sleep(1)
    
    # account入力
    element_account = driver.find_elements(By.NAME, 'text')[0]
    element_account.send_keys(account)
    time.sleep(1) 
    # 次へ
    element_account.send_keys(Keys.RETURN)
    time.sleep(1) 
    
    # パスワード入力
    element_pass = driver.find_elements(By.NAME, 'password')[0]
    element_pass.send_keys(password)
    time.sleep(1) 
    # ログインボタンクリック
    element_login = driver.find_elements(By.XPATH, '//*[@data-testid="LoginForm_Login_Button"]')[0]
    element_login.click()
    time.sleep(1) 
8
8
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?