0

When I run the code, it opens the browser, but I want to click the reject all button when the cookie banner comes from YouTube. I tried using class and a link text. It did not work.

I will appreciate any help.

enter image description here enter image description here

from selenium import webdriver
from selenium.webdriver.common.by import By

url = 'https://www.youtube.com/'
driver = webdriver.Chrome(executable_path="C:\\Users\\donner\\Downloads\\chromedriver_win32\\chromedriver.exe")

driver.get(url)

driver.implicitly_wait(100)
continue_link = driver.find_element(By.LINK_TEXT, "Reject all")
continue_link.click()

driver.implicitly_wait(100)
content = driver.find_element(By.CLASS_NAME, '.style-scope.ytd-button-renderer.style-primary size-default')
content.click()
  • I can not see the cookie banner it's not implemented in my region yet. Can you share the `HTML` source for the banner, also check it could be inside the `iframe` – Akzy Jun 12 '22 at 12:29
  • Hello @Akzy, I have attached a screen of the banner html reject all button – GoogleTagger Jun 13 '22 at 20:45
  • I have added the answer, let us know this does not work – Akzy Jun 15 '22 at 08:26
  • Hey @Akzy I tried it, it did not work. Do you have any selenium webdriver automation code example? For example, going to a website and clicking on different buttons using class or css... – GoogleTagger Jun 15 '22 at 17:54
  • What error are you getting ? – Akzy Jun 16 '22 at 10:50

1 Answers1

0

Try the below xpath with the given code

//*[normalize-space()='Reject all']

OR

//*[contains(text(),'Reject all')]

The Code:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "myXpath")))

element.click();

The above soultion work if there is no iframe

In case there is iframe, than you need to follow below

WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"iframe")))
driver.find_element(By.XPATH, "yourXpath").click()
Akzy
  • 1,817
  • 1
  • 7
  • 19