Search code examples
pythonseleniumxpathcss-selectorswebdriverwait

How to navigate past the popup ad using Selenium


I'm trying to navigate through oddschecker website using selenium in python with chrome webdriver but an add comes up. See image:

enter image description here

Is there a line of code I can use to get rid of the ad and access the site without having to click on the cross in top right corner manually?

Code trials:

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
mylink = 'https://www.oddschecker.com'
driver.get(mylink)

Any help would be greatly appreciated.


Solution

  • To navigate past the popup ad the only way is to click and close the popup ad element inducing WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

    • Using CSS_SELECTOR:

      driver.get("https://www.oddschecker.com/")
      # click and close the cookie banner
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[class^='CookieBannerAcceptButton']"))).click()
      # click and close the popup ad
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Close Offers Modal'][class^='CloseButtonDesktop']"))).click()
      
    • Using XPATH:

      driver.get("https://www.oddschecker.com/")
      # click and close the cookie banner
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='OK']"))).click()
      # click and close the popup ad
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Close Offers Modal' and starts-with(@class, 'CloseButtonDesktop')]"))).click()
      
    • Note: You have to add the following imports :

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