Selenium webdriver: How to delete/flag spam comments on YouTube platform (without API)

Viewed 27

I've been trying to flag/report a list of spam comments in a particular YouTube video.

For that I've been using this code on Python, which loads my previous profile so I log in with my account:

URL = "https://www.youtube.com/watch? 
v=dvecqwfU6xw&lc=Ugxw_nsUNUor9AUEBGp4AaABAg.9fDfvkgiqtW9fDkE2r6Blm"
soup = BeautifulSoup(requests.get(URL).content, "html.parser")

options = webdriver.ChromeOptions()
user = pathlib.Path().home()
print(user)

options.add_argument(f"user-data-dir={user}/AppData/Local/Google/Chrome/User Data/")

driver= webdriver.Chrome('chromedriver.exe',chrome_options=options)
driver.get(URL)
wait=WebDriverWait(driver, 100)

comment_box = '//*[@id="comment"]'
reply_box ='//*[@id="replies"]'

while(True): 
      driver.execute_script("window.scrollBy(0, 200);")
  try:
    reply_box = driver.find_element(By.XPATH, reply_box)
    print(reply_box.text)
    break
  except:
    pass

# resp = driver.request('POST', 'https://www.youtube.com/youtubei/v1/flag/get_form?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8&prettyPrint=false')
# print(resp.text)

button = wait.until(EC.presence_of_element_located((By.XPATH,'//*[@id="button"]')))
driver.execute_script("arguments[0].click();", button)

The problem comes with opening the menu, I believe since you have to hover over the 3 dots menu it would then appear as the clickable menu so I never get to open the actual menu to report/flag the comment.

1 Answers

My mistake was not to take full Xpath path.... It works perfectly like this, THANKS

options = webdriver.ChromeOptions()
user = pathlib.Path().home()
print(user)

options.add_argument(f"user-data-dir={user}/AppData/Local/Google/Chrome/User Data/")
options.add_argument('--headless')

driver= webdriver.Chrome('chromedriver.exe',chrome_options=options)
driver.get(URL)
wait=WebDriverWait(driver, 100)

comment_box = '//*[@id="comment"]'
reply_box ='//*[@id="replies"]'

while(True): 
  driver.execute_script("window.scrollBy(0, 200);")
  try:
    reply_box = driver.find_element(By.XPATH, reply_box)
    print(reply_box.text)
    break
  except:
    pass

option_button = '/html/body/ytd-app/div[1]/ytd-page-manager/ytd-watch-flexy/div[5]/div[1]/div/div[2]/ytd-comments/ytd-item-section-renderer/div[3]/ytd-comment-thread-renderer[1]/div/ytd-comment-replies-renderer/div[2]/ytd-comment-renderer/div[3]/div[3]/ytd-menu-renderer/yt-icon-button/button'
option_button = wait.until(EC.presence_of_element_located((By.XPATH, option_button)))
driver.execute_script("arguments[0].click();", option_button)

report_button = '/html/body/ytd-app/ytd-popup-container/tp-yt-iron-dropdown/div/ytd-menu-popup-renderer/tp-yt-paper-listbox/ytd-menu-service-item-renderer/tp-yt-paper-item/yt-formatted-string'
report_button = wait.until(EC.presence_of_element_located((By.XPATH,report_button)))
driver.execute_script("arguments[0].click();", report_button)

report_button_spam = '/html/body/ytd-app/ytd-popup-container/tp-yt-paper-dialog/yt-report-form-modal-renderer/tp-yt-paper-dialog-scrollable/div/div/yt-options-renderer/div/tp-yt-paper-radio-group/tp-yt-paper-radio-button[1]/div[1]'
report_button_spam = wait.until(EC.presence_of_element_located((By.XPATH, report_button_spam)))
driver.execute_script("arguments[0].click();", report_button_spam)

report_button_send = '/html/body/ytd-app/ytd-popup-container/tp-yt-paper-dialog/yt-report-form-modal-renderer/div/yt-button-renderer[2]/a/tp-yt-paper-button'
report_button_send = wait.until(EC.presence_of_element_located((By.XPATH, report_button_send)))
driver.execute_script("arguments[0].click();", report_button_send)

popup_button_done = '/html/body/ytd-app/ytd-popup-container/tp-yt-paper-dialog[2]/yt-confirm-dialog-renderer/div[2]/div[2]/yt-button-renderer[3]/a/tp-yt-paper-button'
popup_button_done = wait.until(EC.presence_of_element_located((By.XPATH, popup_button_done)))
print(popup_button_done.text)
Related