1

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()

Was able to press this one But not this one

3
  • 1
    good question, i tried to use css selectors, didnt work out either, you might need to use an explicit wait, to ensure it is in view Commented Nov 30, 2020 at 16:54
  • Unfortunately no luck there either. the command: wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "#introAgreeButton"))) didn't even find the object either. Commented Nov 30, 2020 at 17:05
  • 1
    Is it any iframe? Commented Nov 30, 2020 at 17:31

1 Answer 1

2

Maybe you can automate pressing it with your mouse:

pyautogui.moveTo(100, 150) # Move the mouse to XY coordinates.
pyautogui.click() 

I hope this is helpful!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.