I have been trying to web scrape a youtube video but each time I launch the google driver I get presented with a pop-up. I tried to work around it by trying to make selenium to press for me. I was able with the first one. But the second one was more tricky.
- I tried to press it using XPath, both the normal and the full detailed one
- I then tried to press it using name finder. But no success either.
Is there another workaround or is there something wrong or what I can improve with my code? To clarify, I have problems with the second image (i agree button). The goal of this code is to sort the comments by new. Is there a better way to do it in selenium?
code:
import sys, unittest, time, datetime
import urllib.request, urllib.error, urllib.parse
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import InvalidArgumentException
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
from selenium.webdriver import ActionChains
options = webdriver.ChromeOptions()
options.add_argument('--lang=en')
driver = webdriver.Chrome(executable_path=r'C:\Users\caspe\OneDrive\Documents\Övrigt\Kodning\Email\chromeDriver\chromedriver.exe', chrome_options=options)
driver.get("https://www.youtube.com/watch?v=EV6PLN_8RBw")
time.sleep(5)
driver.find_elements_by_xpath("/html/body/ytd-app/ytd-popup-container/paper-dialog/yt-upsell-dialog-renderer/div/div[3]/div[1]/yt-button-renderer")[0].click()
time.sleep(5)
#This Doesn't work
buttons = driver.find_elements_by_xpath("//*[contains(text(), 'I agree')]")
for btn in buttons:
btn.click()

