Targeting Accept Policy With Selenium

Question:

I am trying to target a button from dekudeals using selenium.

Exactly what I’m trying to do is to target the accept button from the site’s policy widnow.

Button to click](https://i.stack.imgur.com/I62AI.png).

So far I have tried:

try:
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable(
        (By.XPATH, "//button[text()='Accept']"))).click()
finally:
    print('Not found :(')
    driver.quit()
try:
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable(
        (By.XPATH, "//*[@id='notice']/div[4]/button[2]"))).click()
finally:
    print('Not found :(')
    driver.quit()
try:
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable(
        (By.XPATH, "//button[@arial-label='Accept']"))).click()
finally:
    print('Not found :(')
    driver.quit()
try:
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable(
        (By.CLASS_NAME, "message-component message-button no-children focusable sp_choice_type_11 last-focusable-el"))).click()
finally:
    print('Not found :(')
    driver.quit()
try:
    WebDriverWait(driver, 10).until(
        EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@name='networknheader13924283968_MOAT_IFRAME']")))
    WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "//button[text()='Accept']"))).click()
finally:
    print('Not found :(')
    driver.quit()
try:
    WebDriverWait(driver, 10).until(
        EC.frame_to_be_available_and_switch_to_it((By.XPATH, '/html/iframe')))
    print('done')
    WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CLASS_NAME, "message-component message-button no-children focusable sp_choice_type_11 last-focusable-el"))).click()
finally:
    print('Not found :(')
    driver.quit()

Then i found out about iframes so I have tried:

try:
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'sp_message_iframe_751684')))
    print('done')
    WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "//*[@id='notice']/div[4]/button[2]"))).click()
finally:
    print('Not found :(')
    driver.quit()

And all XPATHs, CLASS_NAMEs I could come up with.

Can someone please tell me how to target the Accept button?

FULL CODE:

from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

service = Service(executable_path=".../Driver/chromedriver")
driver = webdriver.Chrome(service=service)
driver.get('https://www.dekudeals.com/items/1-crosswords-sudokus-bundle')
try:
    WebDriverWait(driver, 10).until(
        EC.frame_to_be_available_and_switch_to_it((By.XPATH, '/html/iframe')))
    print('done')
    WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.XPATH, "//button[@arial-label='Accept']"))).click()
finally:
    print('Not found :(')
    driver.quit()
Asked By: Ɓukasz Zedler

||

Answers:

Here is one way of clicking that button:

### do your imports
##[...]
wait = WebDriverWait(driver, 25)

url = 'https://www.dekudeals.com/items/1-crosswords-sudokus-bundle'
driver.get(url) 
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//*[@id="sp_message_iframe_751684"]')))
accept_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@title='Accept']")))
accept_button.click()
print('accepted')
driver.switch_to.default_content() ## switch out of iframe

You can find Selenium documentation here.

Answered By: Barry the Platipus
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.