YouTubeのコメントを一括削除するコードを書いているのですが、ループの部分で
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
というエラーが出ました。押下する要素をボタンの親にするなど対策してみたものの効果がありません。解決策が分かる方は教えていただきたいです。
ソースコード
python
1from selenium.webdriver.support.ui import WebDriverWait 2from selenium.webdriver.support import expected_conditions as EC 3from selenium.webdriver.common.by import By 4from selenium import webdriver 5import chromedriver_binary 6import time 7from selenium.common.exceptions import NoSuchElementException 8 9wait_time = 30 10 11# 画面遷移関数 12url = "https://www.youtube.com/feed/history/comment_history" 13 14 15def page_move(url): 16 WebDriverWait(driver, wait_time).until( 17 EC.presence_of_element_located((By.XPATH, url)) 18 ) 19 20 21def insert_pw(xpath, str): 22 driver.find_element_by_xpath(xpath).send_keys(str) 23 24 25def click(xpath): 26 driver.find_element_by_xpath(xpath).click() 27 28 29# YouTubeコメントページに移動 30driver = webdriver.Chrome() 31driver.implicitly_wait(1) 32driver.get(url) 33 34# YouTubeログインページに移動 35login_button = '/html/body/ytd-app/div/div/ytd-masthead/div[3]/div[3]/div[2]/ytd-button-renderer/a/paper-button' 36click(login_button) 37 38time.sleep(1) 39 40# Googleログイン 41# ID入力 42login_id = "hogehogehogehoge@gmail.com" 43login_id_xpath = '/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div/div[1]/div/div[1]/input' 44login_id_button = '/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[2]/div/div[1]/div/div' 45insert_pw(login_id_xpath, login_id) 46click(login_id_button) 47 48time.sleep(1) 49 50# パスワード入力 51login_pw = "hoge1234" 52login_pw_xpath = '/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div[1]/div/div/div/div/div[1]/div/div[1]/input' 53login_pw_button = '/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[2]/div/div[1]/div/div' 54insert_pw(login_pw_xpath, login_pw) 55time.sleep(1) 56click(login_pw_button) 57 58time.sleep(3) 59 60comment_div = '/html/body/ytd-app/div/ytd-page-manager/ytd-browse/ytd-two-column-browse-results-renderer/div[1]/ytd-section-list-renderer/div[2]/ytd-item-section-renderer/div[3]' 61num_of_comments = driver.find_element_by_xpath(comment_div) 62print(num_of_comments) 63 64for num in range(100): 65 menu_bar = '/html/body/ytd-app/div/ytd-page-manager/ytd-browse/ytd-two-column-browse-results-renderer/div[1]/ytd-section-list-renderer/div[2]/ytd-item-section-renderer/div[3]/ytd-comment-history-entry-renderer[1]/div[2]/ytd-menu-renderer' 66 time.sleep(1) 67 click(menu_bar) 68 time.sleep(1) 69 delete_button = '/html/body/ytd-app/ytd-popup-container/iron-dropdown/div/ytd-menu-popup-renderer/paper-listbox/ytd-menu-navigation-item-renderer[2]' 70 delete_button_alt = '/html/body/ytd-app/ytd-popup-container/iron-dropdown/div/ytd-menu-popup-renderer/paper-listbox/ytd-menu-navigation-item-renderer' 71 try: 72 driver.find_element_by_xpath(delete_button) 73 except NoSuchElementException: 74 delete_button = delete_button_alt 75 click(delete_button) 76 delete_confirm = '/html/body/ytd-app/ytd-popup-container/paper-dialog/yt-confirm-dialog-renderer/div[2]/div/yt-button-renderer[2]/a/paper-button' 77 delete_button_alt = '/html/body/ytd-app/ytd-popup-container/paper-dialog/yt-confirm-dialog-renderer/div[2]/div/yt-button-renderer[2]/a/paper-button' 78 click(delete_confirm) 79 time.sleep(1) 80 81driver.close() 82driver.quit() 83
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。