问题描述
我试图在JavaScript页面上循环浏览162个国家/地区排名链接,然后单击每个国家/地区.对于前13个左右的国家/地区链接,它们起作用,但是一旦我到达比利时(送或取)周围,我就会被 ElementClickInterceptedException击中:消息:元素点击被拦截:元素< iframe class =" js-延迟加载"data-src ="https://assets.weforum.org/static/reports/gender-gap-report-2021/v8/index.html" ...
.在脚本的前面,我处理过一个iframe,但是我不确定在出现iframe的情况下在循环中查找并切换到iframe时需要了解和要做的事情.这是我的代码:
I'm trying to to loop through 162 links of country rankings on a JavaScript page and click in and out of each country. 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"...
. 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. 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}")
您可以直接在元素上调用click来绕过重叠的元素.
You can invoke click directly on the element to bypass the overlapping element.
这篇关于ElementClickInterceptedException:消息:拦截了元素单击-如何在for循环中找到并切换到iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!