Selenium 测试被弹出窗口打断

Selenium test getting interrupted by a popup

我正在尝试 运行 此网页上的一些练习测试,该网页打印团队在 table 中的当前位置:

https://www.premierleague.com/tables

但每次我 运行 脚本时,我总是被弹出窗口打断,似乎无法让 Selenium 单击。

我试过在点击之前添加等待,但它一直返回相同的错误

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>

这是我的代码:

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")

我能够通过在找到元素后添加隐式等待来解决问题。通过要求 Selenium 等到元素出现,我能够修复错误。我认为通过 ID 而不是 XPath 选择元素也有帮助。

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