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

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

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

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

selenium

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

Q&A

1回答

858閲覧

jupyter notebook でseleniumが機能しなくなってしまいました。

artless

総合スコア0

Python

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

selenium

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

0グッド

0クリップ

投稿2022/11/07 02:40

前提

jupyter notebook でseleniumを利用しております。
以前までは使えていたのですが、ある日から機能しなくなりました。

素人で大変恐れ入りますが、ご指摘いただけますと幸いです。

from selenium import webdriver from selenium.common.exceptions import WebDriverException from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By import time import random def login(): login_url = 'https://www.instagram.com/accounts/login/?source=auth_switcher' driver.get(login_url) #暗示的に10秒待つ driver.implicitly_wait(10) f = open('insta.txt','a') f.write("instagramにアクセスしました\n") f.close() time.sleep(1) #メアドと、パスワードを入力 driver.find_element_by_name('username').send_keys('*********') time.sleep(1) driver.find_element_by_name('password').send_keys('*********') time.sleep(1) #ログインボタンを押す driver.find_element_by_class_name('L3NKy').click() time.sleep(1) #ログインページから離れるまで待つ(title=='instagramまで) WebDriverWait(driver, 15).until(EC.title_is('Instagram')) def tagsearch(tag): tagurl = 'https://www.instagram.com/explore/tags/' driver.get(tagurl + tag) time.sleep(1) def pushnice(num_nice, nice_count, nice_max): target = driver.find_elements_by_class_name('_9AhH0')[10] actions = ActionChains(driver) actions.move_to_element(target) actions.perform() f = open('insta.txt','a') f.write("最新の投稿まで画面を移動しました\n") f.close() time.sleep(1) try: driver.find_elements_by_class_name('_9AhH0')[9].click() time.sleep(random.randint(2, 10)) f = open('insta.txt','a') f.write("投稿をクリックしました\n") f.close() time.sleep(1) driver.find_element_by_class_name('fr66n').click() f = open('insta.txt','a') f.write("投稿をいいねしました\n") f.close() time.sleep(1) except WebDriverException: f = open('insta.txt','a') f.write("エラーが発生しました\n") f.close() return for i in range(random.randint(3, 5)): try: driver.find_element_by_class_name('coreSpriteRightPaginationArrow').click() f = open('insta.txt','a') f.write("次の投稿へ移動しました\n") f.close() time.sleep(random.randint(random.randint(2, 5), random.randint(10, 15))) except WebDriverException: f = open('insta.txt','a') f.write("2つ目の位置でエラーが発生しました\n") f.close() continue try: driver.find_element_by_class_name('fr66n').click() f = open('insta.txt','a') f.write("投稿をいいねしました\n") f.close() time.sleep(2) except WebDriverException: f = open('insta.txt','a') f.write("3つ目の位置でエラーが発生しました\n") f.close() if __name__ == '__main__': taglist = ['f4f', 'l4l','like4like','followme', 'follow', 'いいね返し'] nice_count = [0] # いいねの回数 nice_max = 30 driver = webdriver.Chrome('C:/Users/R02156/Desktop/python/chromedriver.exe') time.sleep(1) login() while nice_count[0] < nice_max: # タグリストからランダムに検索 tagsearch(random.choice(taglist)) # 3~10回いいねを押す pushnice(random.randint(9,10), nice_count, nice_max) print("残りのいいね回数 %d" % (nice_max-nice_count[0]))

実現したいこと

ここに実現したいことを箇条書きで書いてください。

  • [seleniumによるpythonの自動いいね ]

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

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".L3NKy"} (Session info: chrome=107.0.5304.88)

該当のソースコード

NoSuchElementException Traceback (most recent call last) Input In [35], in <cell line: 93>() 99 driver = webdriver.Chrome('C:/Users/R02156/Desktop/python/chromedriver.exe') 100 time.sleep(1) --> 101 login() 103 while nice_count[0] < nice_max: 104 # タグリストからランダムに検索 105 tagsearch(random.choice(taglist)) Input In [35], in login() 24 time.sleep(1) 26 #ログインボタンを押す ---> 27 driver.find_element_by_class_name('L3NKy').click() 28 time.sleep(1) 30 #ログインページから離れるまで待つ(title=='instagramまで) File ~\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py:766, in WebDriver.find_element_by_class_name(self, name) 744 """ 745 Finds an element by class name. 746 (...) 759 element = driver.find_element_by_class_name('foo') 760 """ 761 warnings.warn( 762 "find_element_by_class_name is deprecated. Please use find_element(by=By.CLASS_NAME, value=name) instead", 763 DeprecationWarning, 764 stacklevel=2, 765 ) --> 766 return self.find_element(by=By.CLASS_NAME, value=name) File ~\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py:1251, in WebDriver.find_element(self, by, value) 1248 by = By.CSS_SELECTOR 1249 value = '[name="%s"]' % value -> 1251 return self.execute(Command.FIND_ELEMENT, { 1252 'using': by, 1253 'value': value})['value'] File ~\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py:430, in WebDriver.execute(self, driver_command, params) 428 response = self.command_executor.execute(driver_command, params) 429 if response: --> 430 self.error_handler.check_response(response) 431 response['value'] = self._unwrap_value( 432 response.get('value', None)) 433 return response File ~\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py:247, in ErrorHandler.check_response(self, response) 245 alert_text = value['alert'].get('text') 246 raise exception_class(message, screen, stacktrace, alert_text) # type: ignore[call-arg] # mypy is not smart enough here --> 247 raise exception_class(message, screen, stacktrace)

試したこと

webdriver.Chromeの更新

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

特にございません

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

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

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

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

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

PondVillege

2022/11/07 02:44

> 以前までは使えていたのですが、ある日から機能しなくなりました。 Webスクレイピングあるある「Webサイトの仕様が変更になった」ではないですか? ログインボタンが押下できないようですが,ボタン選択方法は今も同じで問題ないのでしょうか? また,質問はユーザによる削除が不可で基本的に誤投稿は編集機能の利用にて対応してください.
guest

回答1

0

NoSuchElementException: Message: no such element: Unable to locate element:

は、「指定した要素が無い」というエラーです。
エラーの行の直前で、print(driver.page_source)して、HTMLを見ましょう。
今まで存在したと言うことなら、ページの構成が変わったのでしょうから、今のページに合わせたコードに修正しましょう。

投稿2022/11/07 03:28

otn

総合スコア84553

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問