Home > OS >  Selenium test getting interrupted by a popup
Selenium test getting interrupted by a popup

Time:05-08

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:\my\fav\directory\cookies.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")

CodePudding user response:

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