python - ElementClickInterceptedException:消息:元素单击被拦截 - 如何在 for 循环中定位并切换到 iframe
问题描述
我正在尝试循环浏览 JavaScript 页面上的 162 个国家排名链接,然后点击进出每个国家/地区。对于前 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"...
. 在脚本的前面,我处理了一个 iframe,但我不确定如果出现 iframe,我需要知道和做什么才能在此循环中找到并切换到 iframe。这是我的代码:
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}")
您可以直接在元素上调用 click 以绕过重叠元素。
推荐阅读
- spring - Why isn't RedisMessageListenerContainer starting up?
- node.js - 尝试填充猫鼬模式
- pandas - 加快 pandas DataFrame 中每一行的发布请求
- selenium - 使用量角器如何检查 UI 中是否删除了选项(例如“Ping 测试”)
- c# - 从非 aruba 托管服务器发送来自 Aruba(意大利托管服务)的电子邮件
- excel - excel,计算列组的行组计数数据的方差
- django - django html 查询设置为一个字符串
- android - 为什么用MediaStore访问图片失败,用系统图库选择成功?
- javascript - 将 .js 文件链接到 .html 文件时,JavaScript 不起作用
- java - 可以使用原型模式复制对象的线程吗?