質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

selenium

Selenium(セレニウム)は、ブラウザをプログラムで作動させるフレームワークです。この原理を使うことにより、ブラウザのユーザーテストなどを自動化にすることができます。

Q&A

解決済

1回答

1070閲覧

python3,seleniumで新しく開いた先の要素が見つからない

chacopen

総合スコア1

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

selenium

Selenium(セレニウム)は、ブラウザをプログラムで作動させるフレームワークです。この原理を使うことにより、ブラウザのユーザーテストなどを自動化にすることができます。

0グッド

0クリップ

投稿2021/03/17 01:14

前提・実現したいこと

tumblrというSNSを利用しています.スキ!というお気に入りのようなものを保存しておくために
リブログ(twitterでいうリツイートのようなもの)をしようと思ったのですが,
数が膨大だったため,python,seleniumにて自動化をしようと考えました.

ログイン→スキ!投稿一覧ページ→リブログボタンクリック→投稿先設定→リブログ
というステップを踏みたいのですが,リブログボタンをクリックして新しいページを新規タブで開くと
pythonでもjsでも途端に要素が見つかりませんと出てしまいます.

どこが原因なのか教えていただけないでしょうか.

まだ1投稿をリブログできていないため,繰り返しの実装はまだです.

発生している問題・エラーメッセージ

selenium.common.exceptions.JavascriptException: Message: javascript error: Cannot read property 'click' of undefined
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".tumblelog-select "}

新規ページの再取得はできていることから,タブのスイッチングまではうまくいっているものと思い,その後の要素取得が原因かなと考えています.

該当のソースコード

python

1# coding:utf-8 2from selenium import webdriver 3from selenium.webdriver.common.keys import Keys 4import time 5 6driver = webdriver.Chrome(executable_path='ドライバーのパス') 7 8driver.get("tumblrのURL") 9time.sleep(3) 10 11driver.maximize_window() 12time.sleep(3) 13 14login_id = driver.find_element_by_name("email") 15login_id.send_keys("メールアドレス") 16cmd = "document.querySelector(\"[aria-label='次へ']\").click();" 17driver.execute_script(cmd) 18time.sleep(3) 19 20next_btn = driver.find_element_by_name("password") 21next_btn.send_keys("パスワード") 22cmd = "document.querySelector(\"[aria-label='ログイン']\").click();" 23driver.execute_script(cmd) 24time.sleep(5) 25driver.get("https://www.tumblr.com/likes") 26time.sleep(3) 27 28cmd = "document.querySelector(\"[aria-label='グリッドで表示']\").click();" 29driver.execute_script(cmd) 30time.sleep(3) 31 32cmd = "document.getElementsByClassName(\"CMh87\")[0].click();" 33driver.execute_script(cmd) 34time.sleep(3) 35 36cmd = "window.open(document.querySelector(\"[aria-label='リブログ']\").href, '_blank');" 37driver.execute_script(cmd) 38time.sleep(5) 39 40handle_array = driver.window_handles 41time.sleep(3) 42 43driver.switch_to.window(handle_array[-1]) 44time.sleep(3) 45 46print(driver.page_source) 47 48driver.get(driver.current_url) 49time.sleep(5) 50 51reblog_btn = driver.find_element_by_class_name("tumblelog-select ") 52reblog_btn[0].click() 53# cmd = "document.getElementsByClassName(\"tumblelog-select \")[0].click();" 54# driver.execute_script(cmd) 55time.sleep(3) 56 57cmd = "document.querySelector(\"[aria-label='chacopen55']\").click();" 58driver.execute_script(cmd) 59time.sleep(3) 60 61cmd = "document.querySelector(\"[aria-label='xPAJL']\").click();" 62driver.execute_script(cmd) 63time.sleep(3)

エラーが出るのは
reblog_btn~~
の行からです.

試したこと

・chromeのデベロッパーツールではリブログページを開いた上で,jsでリブログまでおこなうことができました.
・ページ遷移後の停止時間の調整

補足情報(FW/ツールのバージョンなど)

Macbook pro 2018
python 3.9.2

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

回答ではないのですが。
tumblrの規約によると、スクレイピングは禁止事項のようです。

Limitations on Automated Use:

You may not, without express prior written permission, do any of the following while accessing or using the Services: (a) tamper with, or use non-public areas of the Services, or the computer or delivery systems of Tumblr and/or its service providers; (b) probe, scan, or test any system or network (particularly for vulnerabilities), or otherwise attempt to breach or circumvent any security or authentication measures; (c) access or search or attempt to access or search the Services by any means (automated or otherwise) other than through our currently available, published interfaces that are provided by Tumblr (and only pursuant to those terms and conditions) or unless permitted by Tumblr's robots.txt file or other robot exclusion mechanisms; (d) scrape the Services, and particularly scrape Content (as defined below) from the Services; (e) use the Services to send altered, deceptive, or false source-identifying information, including without limitation by forging TCP-IP packet headers or email headers; or (f) interfere with, or disrupt, (or attempt to do so), the access of any Subscriber, host or network, including, without limitation, by sending a virus to, spamming, or overloading the Services, or by scripted use of the Services in such a manner as to interfere with or create an undue burden on the Services.
Terms of Service

投稿2021/03/17 07:54

jeanbiego

総合スコア3966

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

chacopen

2021/03/17 08:16

そうなのですね,, 全く知らないまま実行しようとしていました. 大事になる前に教えていただいてありがとうございます. 次回からはしっかりそういった点も調べてから行動に移すようにします. 回答ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問