LoginSignup
2
2

More than 1 year has passed since last update.

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

Posted at

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

Python 3.6.8
Google Chrome 98.0.4758.102, 96.0.4664.110, 102.0.5005.61
selenium 3.141.0

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

投稿

「input type=file」 要素に投稿したい画像を指定

def send_tweet(text):
    # テキスト入力
    element_text = driver.find_element_by_class_name("notranslate")
    element_text.click()
    element_text.send_keys(text)

    # 画像選択 & アップロード
    image_file_path = "画像ファイルをフルパスで"
    image = glob.glob(image_file_path)
    driver.find_element_by_xpath('//input[@type="file"]').send_keys(image)

    # ツイートボタン
    tweet_button = driver.find_element_by_xpath('//*[@data-testid="tweetButtonInline"]')
    driver.execute_script("arguments[0].click();", tweet_button)

複数の画像を一度に投稿したい場合はsend_keysを複数回実行する

2
2
0

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
2
2