@yumikoro0324 (yumikoro)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Instagramの自動いいね返しについて

解決したいこと

インスタグラムのいいねしてれた人に
自動でいいねを返すツールについてです。
ログインまではできたのですがその先の処理がどこかで間違っているようで
実行結果が言い値返しをした人は0人となっています。

selenium 4.1.0 です。

発生している問題・エラー

    Please use find_element() instead
      loginButton = driver.find_element_by_css_selector("button[type=submit]")
    いいね返しを開始します
    今回は「49人」にいいね返しをおこなう予定です
    開始時刻:2023年01月19日 00:12:07
    ======================
    C:\Users\user\Documents\Mypython\instagram.py:73: DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead
      notification_list = driver.find_elements_by_class_name("_aaiy".replace(" ", "."))
    ===================
    いいね返しが完了しました!
    いいねを押した数:0回
    完了時刻:2023年01月19日 00:12:25

### 該当するソースコード

# アクティビティフィードを開く
driver.get('https://www.instagram.com/accounts/activity/')
time.sleep(random.randint(5,8))

# いいねの人数・回数をリセット
like_total_people_num = 0
like_total_num = 0

# 開始時間を取得
dt_start_now = datetime.datetime.now()

print('いいね返しを開始します')
print(f'今回は「{like_return_number_of_people}人」にいいね返しをおこなう予定です')
print(f"開始時刻:{dt_start_now.strftime('%Y年%m月%d日 %H:%M:%S')}")
print('======================')

# 通知一覧を取得
notification_list = driver.find_elements_by_class_name("_aaiy".replace(" ", "."))

for notification_i, notification in enumerate(notification_list):
   # 設定エリアの回数を超えたらループ処理を終了させる
   if like_total_people_num >= like_return_number_of_people:
       print("指定回数に到達しました")
       break

   # 通知の文章を取得
   notification_text = notification.find_element_by_tag_name("span")

   # ユーザー名を取得
   user_name_text = notification.find_element_by_tag_name("a")
   print(f"▼{notification_i+1}人目:{user_name_text.text}さん")

   # いいね以外の場合、スキップして次の人へ
   if not 'があなたの写真に「いいね!」しました' in notification_text.text:
       print('└いいね以外なのでスキップします')
       time.sleep(random.randint(2,3))
       continue

   # 1つ前にチェックしたユーザーと同じだった場合はスキップ
   if notification_i > 0 and user_name_text.text == previous_user_name:
       print("└1つ前のユーザーと同じなのでスキップします")
       time.sleep(random.randint(2,3))
       continue

   print("└いいね返しします")
   like_total_people_num += 1
   previous_user_name = user_name_text.text

   # ユーザー名をクリックして新しいタブで開く
   # 実行OSがwindowsの場合の処理
   if os.name == "nt":
       ActionChains(driver).move_to_element(user_name_text).key_down(Keys.CONTROL).click().key_up(Keys.CONTROL).perform()

   # 実行OSがMacの場合の処理
   else:
       ActionChains(driver).move_to_element(user_name_text).key_down(Keys.COMMAND).click().key_up(Keys.COMMAND).perform()

# 現在開いてるタブを取得し、新しく開いたタブに切り替える
   handle_array = driver.window_handles
   driver.switch_to.window(handle_array[1])
   time.sleep(random.randint(7,8))

   try:
       # 1つ目の投稿をクリックさせる
       driver.find_element_by_class_name('_aagu').click()
       time.sleep(random.randint(3,5))

       # いいね!ボタンの位置と子要素を取得し、いいね済かどうかのテキストを取得する
       like_check_target_div = driver.find_element_by_class_name("_aamw")
       like_check_target_element = like_check_target_div.find_element_by_class_name('_ab6-')
       like_check_text = like_check_target_element.get_attribute('aria-label')

       if like_check_text == "いいね!":
       # いいねボタンを押す
           like_check_target_div.click()
           print(f'└いいねしました')
           like_total_num += 1
           time.sleep(random.randint(3,5))

       else:
           print(f'└いいね済みなのでスキップします')
           time.sleep(random.randint(2,3))

   except WebDriverException:
       print('└何らかのエラーによりいいねできませんでした')

   finally:
       driver.close()
       time.sleep(random.randint(3,5))

       # 現在開いてるタブを取得し、最初のタブに切り替える
       handle_array = driver.window_handles
       driver.switch_to.window(handle_array[0])

# ブラウザを閉じる
driver.quit()

# 終了時間の取得・出力
dt_finish_now = datetime.datetime.now()
print('===================')
print('いいね返しが完了しました!')
print(f"いいねを押した数:{like_total_num}回")
print(f"完了時刻:{dt_finish_now.strftime('%Y年%m月%d日 %H:%M:%S')}")

自分で試したこと

昨夜は朝までひたすら出てきたエラーについてググって、今日も夕方と夜中時間を使ってほかのコードを試したりした結果です。。。すみません、初心者なのでどうしていいかわからず申し訳ございません。
お忙しい中恐縮ですが宜しくお願い致します。

0 likes

10Answer

isshy様
毎日スレッド見てくださり アドバイス本当にありがとうございます( ノД`)
Tag NameはDeveloper Toolで何回も確認して正しいはずなのに動いてくれないので途方にくれていましたがXPathという方法があったとは!
今日は頂いたリンク+自分で関連する記事を読んで理解できたところを順番に置き換えているところで時間切れになりそうです。
続きは明日やってみて動くか試してみたいです!

selenium4系では奨励していない書き方なのはずいぶん前に気づいており試したんですがエラーが消えなくてなぜだろうとおもったら、BYの宣言をできていませんでした!
こちらも対応していきたいですがOne more next stepなのでまずはXPathの修正と理解を深めていきたいです。
お付き合いくださり感謝です。本日もお疲れさまでした!

1Like

お世話になっております。月末は仕事が立て込んでおり一旦作業ストップさせていただきます。宜しくお願い致します。

1Like

コードはコードブロック内に記載すると見やすくなり回答が付きやすくなると思います。

また、参考にしたサイト等があればそちらも記載すると良いでしょう。

0Like

Comments

  1. @yumikoro0324

    Questioner

    アドバイスありがとうございます。!ブロック内にコードを記載させていただきました!確かに見やすくなりました!

forループ内に各所にあるprintはまったく出力されていないんですかね?
であればnotification_listの要素数が0だったりしてforループ処理がそもそも動いてなさそうですね。
forループの前にnotification_listの要素数をprintする等して確認してみてください。

0Like

Comments

  1. そもそも
    "_aaiy".replace(" ", ".")
    の部分がおかしいですね。
    replaceは文字列を置換するメソッドなので、
    _aaiyという文字列の中の半角スペースを「.」に置き換える、という処理になります。
    つまり、半角スペースがないのでなんの意味もないことをしています。

    driver.find_elements_by_class_name
    こちらは指定したクラス名で複数の要素を取得するメソッドです。
    _aaiyというクラス名が割り当てられている要素をすべて取得するということです。
    (chromeのデベロッパーツールでhtmlを見たときに<div class="*****">みたいになっているときの"*****"の部分)

    なので、取得したい要素のクラス名が間違っていないかデベロッパーツールで調べてみるのがいいかもしれないですね。
  2. tag_nameでの指定はなかなか厳しそうですね。
    XPathでの指定が良いかと思います。
    DevToolsで欲しい要素を右クリックして、
    Copy > Copy XPath
    でXPathをコピーして、
    find_element_by_xpath(コピーしたxpath)
    のように使うんですが、
    以下のようにidやclass名の前方一致、部分一致などで取得することもできます。

    idで前方一致
    driver.find_elements_by_xpath('//*[starts-with(@id,"sample_")]')

    classで部分一致
    driver.find_elements_by_xpath('//*[contains(@class,"sample_")]')


    ちなみに、「find_elements_by_class_name」等「find_elements_by_*」系はselenium4系では非推奨になっています。
    そのため、ところどころで「DeprecationWarning」「UserWarning」が表示されています。

    余裕があるなら以下を参考に
    https://mwkexcelfriend.com/python-selenium/
    find_element(By.CLASS_NAME,”class属性”)
    等直していくことをお勧めします。
  3. xpathはそのまま使うというよりもコピーしたものを少し加工してあげる必要があります。
    前回の参考サイトのxpathで指定の項が参考になると思いますが、
    すみません。余計に混乱させてしまったみたいですね!

    なかなかうまくいかないときは前の状態に戻すのも手です!
    notification_list = driver.find_elements_by_class_name("xh8yej3 x5yr21d".replace(" ", "."))
    はそのまま
    for文の中の
    notification_text = notification.find_element_by_tag_name("x6s0dn4 x1q4h3jn x78zum5 x1y1aw1k xxbr6pl xwib8y2 xbbxn1n x87ps6o x1wq6e7o x1di1pr7 x1h4gsww xux34ky x1ypdohk x1l895ks")
    はtag_nameじゃなくてclass_nameにしてreplaceをつけて
    notification_text = notification.find_element_by_class_name("x6s0dn4 x1q4h3jn x78zum5 x1y1aw1k xxbr6pl xwib8y2 xbbxn1n x87ps6o x1wq6e7o x1di1pr7 x1h4gsww xux34ky x1ypdohk x1l895ks".replace(" ", "."))
    にするとどうでしょう?
    その下のuser_name_textの部分も同様ですね。

    なかなかズバッと回答できなくてすみません!

Issyさま お世話になっております。
いつもご丁寧にご回答感謝いたします。ありがとうございます。

夜分遅くにすみません。 わたしもNotification listが怪しいと思い要素数をPrintしてみました(lenを使いましたが正しいでしょうか?)

# 通知一覧を取得
notification_list = driver.find_elements_by_class_name("_aaiy".replace(" ", "."))
# 通知一覧をPrintで出力
print('リストの要素数' + str(len(notification_list)))

結果↓


今回は「19人」にいいね返しをおこなう予定です
開始時刻:2023年01月20日 05:41:13
======================
C:\Users\user\Documents\Mypython\instagram.py:73: DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead
  notification_list = driver.find_elements_by_class_name("_aaiy".replace(" ", "."))
リストの要素数0
===================
いいね返しが完了しました!
いいねを押した数:0回
完了時刻:2023年01月20日 05:41:27
:point_up:

助けて頂いているのに答えになかなかいきつげずすみません涙

私のPrintが間違ってないならば通知一覧の取得ができていないようです(要素数0)
そのあと("_aaiy".replace(" ", ".")について調べました。
_aaiy を 置き換えているという認識ですが そもそもコード全体にこの_aaiyが他に存在してないです。(Ctl+Fで検索してHitしたのがこのreplaceの部分だけです)
この_aaiyが何者なのかわからずネットでもずっとググっていますがHitせず
ほかにInstagramで通知一覧を取得する方法がないかググっていますが見つからず…。
すみません 今日はここまでとさせていただきます涙

0Like

Issy 様
毎日どうもありがとうございます。
今日頂いたヒントを頂きDeveloperToolで確認したところ 全然違う名前でした。

  loginButton = driver.find_element_by_css_selector("button[type=submit]")
いいね返しを開始します
今回は「69人」にいいね返しをおこなう予定です
開始時刻:2023年01月21日 04:50:42
======================
C:\Users\user\Documents\Mypython\instagram.py:73: DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead
  notification_list = driver.find_elements_by_class_name("xh8yej3 x5yr21d".replace(" ", "."))
リストの要素数64
C:\Users\user\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py:341: UserWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  warnings.warn("find_element_by_* commands are deprecated. Please use find_element() instead")
Traceback (most recent call last):
  File "C:\Users\user\Documents\Mypython\instagram.py", line 83, in <module>
    notification_text = notification.find_element_by_tag_name("span")
  File "C:\Users\user\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 342, in find_element_by_tag_name
    return self.find_element(by=By.TAG_NAME, value=name)
  File "C:\Users\user\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 735, in find_element
    return self._execute(Command.FIND_CHILD_ELEMENT,
  File "C:\Users\user\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 710, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\user\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
  File "C:\Users\user\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"span"}
  (Session info: chrome=109.0.5414.75)
Stacktrace:
Backtrace:
        (No symbol) [0x010B6643]
        (No symbol) [0x0104BE21]
        (No symbol) [0x00F4DA9D]
        (No symbol) [0x00F81342]
        (No symbol) [0x00F8147B]
        (No symbol) [0x00F764C1]
        (No symbol) [0x00F9FDC4]
        (No symbol) [0x00F7641F]
        (No symbol) [0x00FA00D4]
        (No symbol) [0x00FB6B09]
        (No symbol) [0x00F9FB76]
        (No symbol) [0x00F749C1]
        (No symbol) [0x00F75E5D]
        GetHandleVerifier [0x0132A142+2497106]
        GetHandleVerifier [0x013585D3+2686691]
        GetHandleVerifier [0x0135BB9C+2700460]
        GetHandleVerifier [0x01163B10+635936]
        (No symbol) [0x01054A1F]
        (No symbol) [0x0105A418]
        (No symbol) [0x0105A505]
        (No symbol) [0x0106508B]
        BaseThreadInitThunk [0x75B400F9+25]
        RtlGetAppContainerNamedObjectPath [0x77B17BBE+286]
        RtlGetAppContainerNamedObjectPath [0x77B17B8E+238]

リスト要素は取り出せるようになりました!
他にもDeveloperToolを使ってClass名が違うところがありそうなので探してみようと思います!何日もかかってすみません泣
今日はここまでとさせていただきます。 毎日本当にありがとうございます。

0Like

Developer Toolで要素の名前が間違っていたところを複数Updateしましたがまだどこか間違っているようです。。。
違うDivcleassを選んでいるようなのですが まだ正解に行きつけていないのでどの部分か探している最中です。
作業を続けてみます!宜しくお願い致します。


# 開始時間を取得
dt_start_now = datetime.datetime.now()

print('いいね返しを開始します')
print(f'今回は「{like_return_number_of_people}人」にいいね返しをおこなう予定です')
print(f"開始時刻:{dt_start_now.strftime('%Y年%m月%d日 %H:%M:%S')}")
print('======================')

# 通知一覧を取得
notification_list = driver.find_elements_by_class_name("xh8yej3 x5yr21d".replace(" ", "."))
# 通知一覧をPrintで出力
print('リストの要素数' + str(len(notification_list)))
for notification_i, notification in enumerate(notification_list):
   # 設定エリアの回数を超えたらループ処理を終了させる
   if like_total_people_num >= like_return_number_of_people:
       print("指定回数に到達しました")
       break

   # 通知の文章を取得
   notification_text = notification.find_element_by_tag_name("x6s0dn4 x1q4h3jn x78zum5 x1y1aw1k xxbr6pl xwib8y2 xbbxn1n x87ps6o x1wq6e7o x1di1pr7 x1h4gsww xux34ky x1ypdohk x1l895ks")

   # ユーザー名を取得
   user_name_text = notification.find_element_by_tag_name("x193iq5w xeuugli x1fj9vlw x13faqbe x1vvkbs xt0psk2 x1i0vuye xvs91rp x1xlr1w8 x5n08af x10wh9bi x1wdrske x8viiok x18hxmgj")
   print(f"▼{notification_i+1}人目:{user_name_text.text}さん")

   # いいね以外の場合、スキップして次の人へ
   if not 'があなたの写真に「いいね!」しました' in notification_text.text:
       print('└いいね以外なのでスキップします')
       time.sleep(random.randint(2,3))
       continue

   # 1つ前にチェックしたユーザーと同じだった場合はスキップ
   if notification_i > 0 and user_name_text.text == previous_user_name:
       print("└1つ前のユーザーと同じなのでスキップします")
       time.sleep(random.randint(2,3))
       continue

   print("└いいね返しします")
   like_total_people_num += 1
   previous_user_name = user_name_text.text

   # ユーザー名をクリックして新しいタブで開く
   # 実行OSがwindowsの場合の処理
   if os.name == "nt":
       ActionChains(driver).move_to_element(user_name_text).key_down(Keys.CONTROL).click().key_up(Keys.CONTROL).perform()

   # 実行OSがMacの場合の処理
   else:
       ActionChains(driver).move_to_element(user_name_text).key_down(Keys.COMMAND).click().key_up(Keys.COMMAND).perform()

   # 現在開いてるタブを取得し、新しく開いたタブに切り替える
   handle_array = driver.window_handles
   driver.switch_to.window(handle_array[1])
   time.sleep(random.randint(7,8))

   try:
       # 1つ目の投稿をクリックさせる
       driver.find_element_by_class_name('_aagu').click()
       time.sleep(random.randint(3,5))

       # いいね!ボタンの位置と子要素を取得し、いいね済かどうかのテキストを取得する
       like_check_target_div = driver.find_element_by_class_name("_aamw")
       like_check_target_element = like_check_target_div.find_element_by_class_name('_ab6-')
       like_check_text = like_check_target_element.get_attribute('aria-label')

       if like_check_text == "いいね!":
       # いいねボタンを押す
           like_check_target_div.click()
           print(f'└いいねしました')
           like_total_num += 1
           time.sleep(random.randint(3,5))

       else:
           print(f'└いいね済みなのでスキップします')
           time.sleep(random.randint(2,3))

   except WebDriverException:
       print('└何らかのエラーによりいいねできませんでした')

   finally:
       driver.close()
       time.sleep(random.randint(3,5))

       # 現在開いてるタブを取得し、最初のタブに切り替える
       handle_array = driver.window_handles
       driver.switch_to.window(handle_array[0])

# ブラウザを閉じる
driver.quit()

# 終了時間の取得・出力
dt_finish_now = datetime.datetime.now()
print('===================')
print('いいね返しが完了しました!')
print(f"いいねを押した数:{like_total_num}回")
print(f"完了時刻:{dt_finish_now.strftime('%Y年%m月%d日 %H:%M:%S')}")

実行結果

    loginButton = driver.find_element_by_css_selector("button[type=submit]")
いいね返しを開始します
今回は「31人」にいいね返しをおこなう予定です
開始時刻:2023年01月23日 04:59:44
======================
C:\Users\user\Documents\Mypython\instagram.py:73: DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead
  notification_list = driver.find_elements_by_class_name("xh8yej3 x5yr21d".replace(" ", "."))
リストの要素数27
C:\Users\user\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py:341: UserWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  warnings.warn("find_element_by_* commands are deprecated. Please use find_element() instead")
Traceback (most recent call last):
  File "C:\Users\user\Documents\Mypython\instagram.py", line 83, in <module>
    notification_text = notification.find_element_by_tag_name("x6s0dn4 x1q4h3jn x78zum5 x1y1aw1k xxbr6pl xwib8y2 xbbxn1n x87ps6o x1wq6e7o x1di1pr7 x1h4gsww xux34ky x1ypdohk x1l895ks")
  File "C:\Users\user\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 342, in find_element_by_tag_name
    return self.find_element(by=By.TAG_NAME, value=name)
  File "C:\Users\user\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 735, in find_element
    return self._execute(Command.FIND_CHILD_ELEMENT,
  File "C:\Users\user\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 710, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\user\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
  File "C:\Users\user\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"x6s0dn4 x1q4h3jn x78zum5 x1y1aw1k xxbr6pl xwib8y2 xbbxn1n x87ps6o x1wq6e7o x1di1pr7 x1h4gsww xux34ky x1ypdohk x1l895ks"}
  (Session info: chrome=109.0.5414.75)
Stacktrace:
Backtrace:
        (No symbol) [0x00D56643]
        (No symbol) [0x00CEBE21]
        (No symbol) [0x00BEDA9D]
        (No symbol) [0x00C21342]
        (No symbol) [0x00C2147B]
        (No symbol) [0x00C164C1]
        (No symbol) [0x00C3FDC4]
        (No symbol) [0x00C1641F]
        (No symbol) [0x00C400D4]
        (No symbol) [0x00C56B09]
        (No symbol) [0x00C3FB76]
        (No symbol) [0x00C149C1]
        (No symbol) [0x00C15E5D]
        GetHandleVerifier [0x00FCA142+2497106]
        GetHandleVerifier [0x00FF85D3+2686691]
        GetHandleVerifier [0x00FFBB9C+2700460]
        GetHandleVerifier [0x00E03B10+635936]
        (No symbol) [0x00CF4A1F]
        (No symbol) [0x00CFA418]
        (No symbol) [0x00CFA505]
        (No symbol) [0x00D0508B]
        BaseThreadInitThunk [0x75AE00F9+25]
        RtlGetAppContainerNamedObjectPath [0x77797BBE+286]
        RtlGetAppContainerNamedObjectPath [0x77797B8E+238]


0Like

おはようございます!
Xpathで試したのですがうまくいかなかったです。涙
リストの要素数が0になってしまいました。
XpathのCopyの仕方がもしかしたら間違っているのかもしれません。
以前はdiv classの該当する箇所の名前をCtl+Fで検索したら
同じ要素のHiTがいくつか出てきましたが今回同様の検索をしてもHITは1のみでした。(自分がXpathで書き換えたところのみ)

申し訳ございません、Xpathの勉強が足りてないようなのですがもう朝になってしまったので
今日はここまでにさせて頂きます。

# 開始時間を取得
dt_start_now = datetime.datetime.now()

print('いいね返しを開始します')
print(f'今回は「{like_return_number_of_people}人」にいいね返しをおこなう予定です')
print(f"開始時刻:{dt_start_now.strftime('%Y年%m月%d日 %H:%M:%S')}")
print('======================')

# 通知一覧を取得
notification_list = driver.find_elements_by_xpath('//*[@id="mount_0_0_NQ"]/div/div/div/div[1]/div/div/div/div[1]/div[1]/div[1]/div/div/div[2]/div/div/div'.replace(" ", "."))

# 通知一覧をPrintで出力
print('リストの要素数' + str(len(notification_list)))
# notificationという定義した変数に"notificatoon_list"に入っている要素をnotiification_i(0から始まるインデックス)に格納する。
for notification_i, notification in enumerate(notification_list):
   # 設定エリアの回数を超えたらループ処理を終了させる
   if like_total_people_num >= like_return_number_of_people:
       print("指定回数に到達しました")
       break

   # 通知の文章を取得
   notification_text = notification.find_element_by_xpath('//*[@id="mount_0_0_3t"]/div/div/div/div[1]/div/div/div/div[1]/div[1]/div[1]/div/div/div[2]/div/div/div/div/div[2]/div[2]/div[5]/div[2]')

   # ユーザー名を取得
   user_name_text = notification.find_element_by_xpath('//*[@id="mount_0_0_ze"]/div/div/div/div[1]/div/div/div/div[1]/div[1]/div[1]/div/div/div[2]/div/div/div/div/div[2]/div[2]/div[7]/div[2]/span/a')
   print(f"▼{notification_i+1}人目:{user_name_text.text}さん")

↓実行結果

C:\Users\user\Documents\Mypython>python instagram.py
C:\Users\user\Documents\Mypython\instagram.py:31: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  driver = webdriver.Chrome(ChromeDriverManager().install())

DevTools listening on ws://127.0.0.1:62568/devtools/browser/94a2d3f4-7830-4b10-9b13-028d4fd57c96
C:\Users\user\Documents\Mypython\instagram.py:38: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  driver.find_element_by_name('username').send_keys(account_id)
C:\Users\user\Documents\Mypython\instagram.py:40: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  driver.find_element_by_name('password').send_keys(account_pass)
[11716:844:0125/065057.312:ERROR:device_event_log_impl.cc(215)] [06:50:57.311] USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connection: システムに接続されたデバイスが機能していません。 (0x1F)
[11716:844:0125/065057.316:ERROR:device_event_log_impl.cc(215)] [06:50:57.316] USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connection: システムに接続されたデバイスが機能していません。 (0x1F)
[11716:844:0125/065057.318:ERROR:device_event_log_impl.cc(215)] [06:50:57.318] USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connection: システムに接続されたデバイスが機能していません。 (0x1F)
C:\Users\user\Documents\Mypython\instagram.py:44: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  loginButton = driver.find_element_by_css_selector("button[type=submit]")
いいね返しを開始します
今回は「35人」にいいね返しをおこなう予定です
開始時刻:2023年01月25日 06:51:13
======================
C:\Users\user\Documents\Mypython\instagram.py:73: DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead
  notification_list = driver.find_elements_by_xpath('//*[@id="mount_0_0_NQ"]/div/div/div/div[1]/div/div/div/div[1]/div[1]/div[1]/div/div/div[2]/div/div/div'.replace(" ", "."))
リストの要素数0
===================
いいね返しが完了しました!
いいねを押した数:0回
完了時刻:2023年01月25日 06:51:29

0Like

ISSY様
おはようございます。毎日様子を見に来てくださってお忙しい中アドバイスくださり本当に感謝です。ググってもわかりかねる部分が多いため、アドバイスをして下さる方がいて有難い次第です。ありがとうございます。

XpathはそのままCopyするのではなく少し修正して…
→いろいろなサイトを読んだりし基本は理解はできましたが今回のWEBPageが初心者には複雑すぎるので難しそうでした 泣 
ごめんなさい。。。

もとの書き方に戻しReplaceに置き換える方法
→試したところリストの要素数はまた見えるようになりましたが別のエラーが出ました。

# 開始時間を取得
dt_start_now = datetime.datetime.now()

print('いいね返しを開始します')
print(f'今回は「{like_return_number_of_people}人」にいいね返しをおこなう予定です')
print(f"開始時刻:{dt_start_now.strftime('%Y年%m月%d日 %H:%M:%S')}")
print('======================')

# 通知一覧を取得
notification_list = driver.find_elements_by_class_name("xh8yej3 x5yr21d".replace(" ", "."))

# 通知一覧をPrintで出力
print('リストの要素数' + str(len(notification_list)))
# notificationという定義した変数に”notificatoon_list"に入っている要素を notiification_i(0から始まるインデックス)に格納する。
for notification_i, notification in enumerate(notification_list):
   # 設定エリアの回数を超えたらループ処理を終了させる
   if like_total_people_num >= like_return_number_of_people:
       print("指定回数に到達しました")
       break

   # 通知の文章を取得
   notification_text = notification.find_element_by_class_name("x6s0dn4 x1q4h3jn x78zum5 x1y1aw1k xxbr6pl xwib8y2 xbbxn1n x87ps6o x1wq6e7o x1di1pr7 x1h4gsww xux34ky x1ypdohk x1l895ks".replace(" ", "."))

   # ユーザー名を取得
   user_name_text =  notification.find_element_by_class_name("x1i10hfl xjbqb8w x6umtig x1b1mbwd xaqea5y xav7gou x9f619 x1ypdohk xt0psk2 xe8uvvx xdj266r x11i5rnm xat24cr x1mh8g0r xexx8yu x4uap5 x18d9i69 xkhd6sd x16tdsg8 x1hl2dhg xggy1nq x1a2a7pz notranslate _a6hd".replace(" ", "."))
   print(f"▼{notification_i+1}人目:{user_name_text.text}さん")

↓実行結果

C:\Users\user\Documents\Mypython>python instagram.py
C:\Users\user\Documents\Mypython\instagram.py:31: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  driver = webdriver.Chrome(ChromeDriverManager().install())

DevTools listening on ws://127.0.0.1:62474/devtools/browser/0145beb4-5c70-43de-aad3-491ab00a842e
C:\Users\user\Documents\Mypython\instagram.py:38: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  driver.find_element_by_name('username').send_keys(account_id)
C:\Users\user\Documents\Mypython\instagram.py:40: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  driver.find_element_by_name('password').send_keys(account_pass)
[19188:17932:0126/045348.390:ERROR:device_event_log_impl.cc(215)] [04:53:48.390] USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connection: システムに接続されたデバイスが機能していません。 (0x1F)
[19188:17932:0126/045348.401:ERROR:device_event_log_impl.cc(215)] [04:53:48.401] USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connection: システムに接続されたデバイスが機能していません。 (0x1F)
[19188:17932:0126/045348.407:ERROR:device_event_log_impl.cc(215)] [04:53:48.407] USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connection: システムに接続されたデバイスが機能していません。 (0x1F)
C:\Users\user\Documents\Mypython\instagram.py:44: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  loginButton = driver.find_element_by_css_selector("button[type=submit]")
いいね返しを開始します
今回は「58人」にいいね返しをおこなう予定です
開始時刻:2023年01月26日 04:54:09
======================
C:\Users\user\Documents\Mypython\instagram.py:73: DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead
  notification_list = driver.find_elements_by_class_name("xh8yej3 x5yr21d".replace(" ", "."))
リストの要素数101
C:\Users\user\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py:446: UserWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  warnings.warn("find_element_by_* commands are deprecated. Please use find_element() instead")
Traceback (most recent call last):
  File "C:\Users\user\Documents\Mypython\instagram.py", line 85, in <module>
    notification_text = notification.find_element_by_class_name("x6s0dn4 x1q4h3jn x78zum5 x1y1aw1k xxbr6pl xwib8y2 xbbxn1n x87ps6o x1wq6e7o x1di1pr7 x1h4gsww xux34ky x1ypdohk x1l895ks".replace(" ", "."))
  File "C:\Users\user\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 447, in find_element_by_class_name
    return self.find_element(by=By.CLASS_NAME, value=name)
  File "C:\Users\user\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 735, in find_element
    return self._execute(Command.FIND_CHILD_ELEMENT,
  File "C:\Users\user\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 710, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\user\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
  File "C:\Users\user\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".x6s0dn4.x1q4h3jn.x78zum5.x1y1aw1k.xxbr6pl.xwib8y2.xbbxn1n.x87ps6o.x1wq6e7o.x1di1pr7.x1h4gsww.xux34ky.x1ypdohk.x1l895ks"}
  (Session info: chrome=109.0.5414.75)
Stacktrace:
Backtrace:
        (No symbol) [0x00D56643]
        (No symbol) [0x00CEBE21]
        (No symbol) [0x00BEDA9D]
        (No symbol) [0x00C21342]
        (No symbol) [0x00C2147B]
        (No symbol) [0x00C164C1]
        (No symbol) [0x00C3FDC4]
        (No symbol) [0x00C1641F]
        (No symbol) [0x00C400D4]
        (No symbol) [0x00C56B09]
        (No symbol) [0x00C3FB76]
        (No symbol) [0x00C149C1]
        (No symbol) [0x00C15E5D]
        GetHandleVerifier [0x00FCA142+2497106]
        GetHandleVerifier [0x00FF85D3+2686691]
        GetHandleVerifier [0x00FFBB9C+2700460]
        GetHandleVerifier [0x00E03B10+635936]
        (No symbol) [0x00CF4A1F]
        (No symbol) [0x00CFA418]
        (No symbol) [0x00CFA505]
        (No symbol) [0x00D0508B]
        BaseThreadInitThunk [0x75AE00F9+25]
        RtlGetAppContainerNamedObjectPath [0x77797BBE+286]
        RtlGetAppContainerNamedObjectPath [0x77797B8E+238]

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".x6s0dn4.x1q4h3jn.x78zum5.x1y1aw1k.xxbr6pl.xwib8y2.xbbxn1n.x87ps6o.x1wq6e7o.x1di1pr7.x1h4gsww.xux34ky.x1ypdohk.x1l895ks"}

このエラーはIframeのせいでfind_element_byができないということを言っているのでしょうか?
このリンクに解決策があるのですが理解までは行きついておらずすみません
今日も朝になってしまったのでここまでとさせていただきます。

0Like

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate elemen
このエラーはそのまま日本語に直すと
そのような要素はありません: 要素が見つかりません
というエラーになります。
つまり、取得しようと指定している
"x6s0dn4 x1q4h3jn x78zum5 x1y1aw1k xxbr6pl xwib8y2 xbbxn1n x87ps6o x1wq6e7o x1di1pr7 x1h4gsww xux34ky x1ypdohk x1l895ks"
のクラス名が付いている要素が無いということですね。

通知一覧の要素は取得できているので、その中身の要素の文章やユーザー名がiframeなんて特殊な事はおそらく無いのでは、と思います。
(実際html構造見ていないので何とも言えませんが。)

classやtagについてはきちんと理解できてますでしょうか?
例えば以下であれば
<div class="aiueo">
「"aiueo"」の部分がclass名
「div」がtag名になります。
tag名はdivの他にもp span等々いろいろあるのでわからないようであれば調べてみてください。

class名、tag名どちらでも要素は取得できると思うので、htmlの構造をよく見ていろいろ試してみて、やりやすい方法を探してみてください。

0Like

Your answer might help someone💌