How to click a button inside a pop-up in selenium (Python)?

Viewed 112

I'm looking to click on a button that appear when you have no cookies from this website, named "accéder au site". Here is the process to have this button :

  1. No cookies
  2. Go to https://www.allocine.fr
  3. Click the "Accepter et accéder gratuitement" button
  4. Then a pop-up for promoting a movie appear with a "accéder au site" button in order to skip it. (the pop-up last for about 5/10s)

I'm using Selenium (Python).

This is the website : https://www.allocine.fr I tried many things such as css selector, partial link text, switching to frame but nothing is working. I also thought that my problem was that I have to wait before the button appears, but this isn't working too.

Here is my code :

from yggtorrentscraper import YggTorrentScraperSelenium
from selenium import webdriver


options = webdriver.ChromeOptions()
options.headless = False
options.add_argument("--log-level=3")
prefs = {"profile.default_content_setting_values.notifications" : 2}
options.add_experimental_option("prefs",prefs)

PATH_CHROME_DRIVER = "C:\\Users\\Kevin\\Desktop\\chromedriver.exe"
driver = webdriver.Chrome(PATH_CHROME_DRIVER, options=options)
driver.get("https://www.allocine.fr")

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

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[1]/div/div/div/div/div/div[2]/div/span/div/button[2]'))).click()

#not working
#WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[2]/div/a'))).click()

Here is also the HTML of the button

<div id="dfp_interstitial">
<div id="dfp_interstitial__top">
    <div id="dfp_interstitial__top-logo" style="background-image: url(&quot;https://lh3.googleusercontent.com/-PBjnn15gFS4/XJzvx16iKdI/AAAAAAAADro/yVpahUmuWwMxthTXopnE0lLgJ0zJxG0qgCK8BGAs/s0/2019-03-28.png&quot;);"></div>
    <div id="dfp_interstitial__top-close">
        accéder au site &nbsp;<span class="dfp_interstitial__arrow-right"></span><span class="dfp_interstitial__arrow-right"></span>
    </div>
</div>
<a id="dfp_interstitial__click" target="_blank">
    <img id="dfp_interstitial__image" src="https://tpc.googlesyndication.com/simgad/5287997328350019355?" style="max-width: 1280px;">
</a>

I have really no idea what I'm doing wrong. Any help appreciated !

1 Answers

Few things :

  1. CSS is preferred over xpath. Note that you are using absolute xpath, which is brittle in nature.

  2. Use explicit waits.

  3. accéder au site is in an iframe, so you'd have to switch to iframe first then you can click on this button.

code :-

driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(30)
wait = WebDriverWait(driver, 20)
driver.get("https://www.allocine.fr/")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[onclick^='Didomi.setUserAgreeToAll']"))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[id^='google_ads_iframe_'][id$='homepage/home_2']")))
wait.until(EC.element_to_be_clickable((By.ID, "dfp_interstitial__top-close"))).click()

Imports :

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