Selenium test getting interrupted by a popup

I am trying to run some practice test on this webpage that prints the current positions of teams in this table:

https://www.premierleague.com/tables

but each time i run the script, I keep getting interrupted by a popup that I can’t seem to get Selenium to click.

I have tried adding a wait before clicking, but it keeps returning the same error

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <li role="tab" tabindex="0" data-tab-index="1">...</li> is not clickable at point (184, 396). Other element would receive the click: <iframe id="google_ads_iframe_/131332370/GeneralTakeover_0" name="google_ads_iframe_/131332370/GeneralTakeover_0" title="3rd party ad content" width="1600" height="900" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" role="region" aria-label="Advertisement" tabindex="0" srcdoc="" data-google-container-id="1" style="border: 0px; vertical-align: bottom;" data-load-complete="true"></iframe>

This is my code:

import pickle
import pprint
import time

from selenium import webdriver

def save_cookies(driver, location):

    pickle.dump(driver.get_cookies(), open(location, "wb"))


def load_cookies(driver, location, url=None):

    cookies = pickle.load(open(location, "rb"))
    driver.delete_all_cookies()
    # have to be on a page before you can add any cookies, any page - does not matter which
    driver.get("https://www.premierleague.com/tables" if url is None else url)
    for cookie in cookies:
        if isinstance(cookie.get('expiry'), float):#Checks if the instance expiry a float 
            cookie['expiry'] = int(cookie['expiry'])# it converts expiry cookie to a int 
        driver.add_cookie(cookie)


def delete_cookies(driver, domains=None):

    if domains is not None:
        cookies = driver.get_cookies()
        original_len = len(cookies)
        for cookie in cookies:
            if str(cookie["domain"]) in domains:
                cookies.remove(cookie)
        if len(cookies) < original_len:  # if cookies changed, we will update them
            # deleting everything and adding the modified cookie object
            driver.delete_all_cookies()
            for cookie in cookies:
                driver.add_cookie(cookie)
    else:
        driver.delete_all_cookies()


# Path where you want to save/load cookies to/from aka C:myfavdirectorycookies.txt
cookies_location = "cookies.txt"

# Initial load of the domain that we want to save cookies for
chrome = webdriver.Chrome()
# wait = WebDriverWait(chrome, 20)
chrome.get("https://www.premierleague.com/tables")

chrome.find_element_by_xpath("//button[normalize-space()='Accept All Cookies']").click()
#popup i am trying to get rid of
chrome.find_element_by_xpath("//a[class='closeBtn']").click()
#after clicking the popup do this next
chrome.find_element_by_xpath("(//li[@data-tab-index='1'])").click()
save_cookies(chrome, cookies_location)
chrome.quit()

# Load of the page you cant access without cookies, this one will fail
chrome = webdriver.Chrome()
chrome.get("https://www.premierleague.com/tables")

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I was able to fix the problem by adding an implicit wait after the finding the element. By asking Selenium to wait until the element appears I was able fix the error. I thinking selecting the element by ID instead of XPath also helped.

button = chrome.find_element_by_id('advertClose')
chrome.implicitly_wait(10)
ActionChains(chrome).move_to_element(button).click(button).perform()


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x