简体   繁体   English

ElementClickInterceptedException:消息:元素单击被拦截 - 如何在 for 循环中定位并切换到 iframe

[英]ElementClickInterceptedException: Message: element click intercepted - how to locate and switch to iframe in a for loop

I'm trying to to loop through 162 links of country rankings on a JavaScript page and click in and out of each country.我正在尝试遍历 JavaScript 页面上的 162 个国家/地区排名链接,然后单击进出每个国家/地区。 For the first 13 or so country links they work, but once I get to around Belgium (give or take), I'm hit with ElementClickInterceptedException: Message: element click intercepted: Element <iframe class="js-lazyload loaded" data-src="https://assets.weforum.org/static/reports/gender-gap-report-2021/v8/index.html"... .对于前 13 个左右的国家/地区链接,它们可以工作,但是一旦我到达比利时附近(给予或接受),我就会遇到ElementClickInterceptedException: Message: element click intercepted: Element <iframe class="js-lazyload loaded" data-src="https://assets.weforum.org/static/reports/gender-gap-report-2021/v8/index.html"... Earlier in the script I handled an iframe, but I'm not sure what I need to know and do in order to find and switch to an iframe in this loop should an iframe arise.在脚本的前面,我处理了一个 iframe,但我不确定如果出现 iframe,我需要知道和做什么才能在此循环中找到并切换到 iframe。 Here's my code:这是我的代码:

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

driver = webdriver.Chrome(executable_path= "C:/work/chromedriver.exe")

def load_list_page(returnlist = False):
    '''
    This function takes you to the list view of country rankings for the Gender Gap Index.
    There's a default option to return all country rankings on the page as a list.
    '''
    driver.get("https://www.weforum.org/reports/global-gender-gap-report-2021/in-full/economy-profiles#economy-profiles")
    
    # bring up list view of countries
    wait=WebDriverWait(driver,10)
    wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "iFrameResizer0")))
    wait.until(EC.element_to_be_clickable((By.XPATH,"//*[name()='svg' and @class='sc-gzOgki ftxBlu']"))).click()
    
    if returnlist:
        list_of_countries = driver.find_elements_by_xpath("//div[@id='root']//a[@class='sc-chPdSV lcYVwX sc-kAzzGY kraFwA']/div[1]/div")

        return list_of_countries

# Collect country names
countries = load_list_page(returnlist=True)
country_names_raw = [i.text for i in countries]
# get all non-empty strings
country_names = [i for i in country_names_raw if len(i)>0]
# extract just the country name using regex
country_names = [re.match(r'\d{,3}. ([\w\s,\'\.]+).*\n', i).group(1) for i in country_names]

# Record the index for the country names that had non-empty strings. These indexes reference WebElements that
# will link to the country profile page. Use these indices to grab the webelements that link to country profiles
# NOTE: I had to add 1 to each index since it seems the link is in the webelement immediately after the webelement with the country text 
link_index = [i+1 for i, j in enumerate(country_names_raw) if len(j) > 0]

# Loop through and click country rankings
for index, link in enumerate(link_index[:14]):
    try:
        countries = load_list_page(returnlist=True)
        countries[link].click()
    except Exception as e:
        print(f"{e}")
        print(f"Error for {country_names[index]}, link index: {link}")

for index, link in enumerate(link_index[:14]):
    try:
        countries = load_list_page(returnlist=True)
        #countries[link].click()
        driver.execute_script("arguments[0].click();", countries[link])
    except Exception as e:
        print(f"{e}")
        print(f"Error for {country_names[index]}, link index: {link}")

You can invoke click directly on the element to bypass the overlapping element.您可以直接在元素上调用 click 以绕过重叠元素。

When iterating through the list of countries,在遍历国家列表时,

for index, link in enumerate(link_index[:14]):  

before clicking on the element scroll that element into the view.在单击元素之前将该元素滚动到视图中。
Java method performing this looks like:执行此操作的Java方法如下所示:

public void scrollElementIntoView(WebElement element){
        waitForElementToBeVisible(element);
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", webElement);
        wait(300);
    }

Python method will be similar. Python方法将类似。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ElementClickInterceptedException:消息:元素点击被拦截: - ElementClickInterceptedException: Message: element click intercepted: ElementClickInterceptedException: 消息: 元素点击被拦截: <label>Selenium 和 Python 无法点击</label>元素 - ElementClickInterceptedException: Message: element click intercepted: Element <label> is not clickable with Selenium and Python Python & Selenium:ElementClickInterceptedException:消息:元素点击拦截错误 - Python & Selenium: ElementClickInterceptedException: Message: element click intercepted error selenium.common.exceptions.ElementClickInterceptedException:消息:元素点击被拦截 - selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted ElementClickInterceptedException:消息:使用 Selenium WebDriver 拦截元素点击 - ElementClickInterceptedException: Message: element click intercepted using Selenium WebDriver selenium.common.exceptions.ElementClickInterceptedException:消息:元素点击被拦截:元素不可点击 Selenium 和 Python - selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable with Selenium and Python 为什么得到这个 selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element - Why getting this selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element ElementClickInterceptedException:消息:元素单击拦截元素不可单击错误单击使用 Selenium 和 Python 的单选按钮 - ElementClickInterceptedException: Message: element click intercepted Element is not clickable error clicking a radio button using Selenium and Python selenium.common.exceptions.ElementClickInterceptedException:消息:使用 Selenium 单击单选按钮时元素单击被截获错误 - selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted error clicking on a radiobutton using Selenium Python 消息:元素点击被拦截 - Message: element click intercepted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM