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