Home > Back-end >  How to navigate past the popup ad using Selenium
How to navigate past the popup ad using Selenium

Time:08-18

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.

CodePudding user response:

This would help you in closing the ad.

driver.get('https://www.oddschecker.com')
time.sleep(4)    
driver.find_element(By.XPATH, "//*[@aria-label='Close Offers Modal']").click()

P.S. I wrote in python. If you are writing in some other language, you may transform this line per the syntax of your language.

CodePudding user response:

You can integrate AdBlocker extension within you chrome driver just like this:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('load-extension='   EXTENSION_PATH)
driver = webdriver.Chrome(DRIVER,chrome_options=chrome_options)
driver.create_options()

CodePudding user response:

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
    
  • Related