首页 > 解决方案 > ElementClickInterceptedException:元素点击被拦截。其他元素会收到点击:Selenium Python

问题描述

我已经看到有关此错误的其他问题,但我的情况是在我的程序中,其他元素应该收到点击。详细信息:网络驱动程序正在通过谷歌搜索滚动,它必须点击它找到的每个网站,但程序阻止了这一点。我怎样才能让它不搜索它点击的上一个站点?

这就是功能。程序正在循环它,在第一个循环之后它向下滚动并发生错误:

def get_info():
browser.switch_to.window(browser.window_handles[2])
description = WebDriverWait(browser, 10).until(
    EC.presence_of_element_located((By.TAG_NAME, "h3"))
).text

site = WebDriverWait(browser, 10).until(
    EC.presence_of_element_located((By.TAG_NAME, "cite"))
)
site.click()

url=browser.current_url
#removes the https:// and the / of the url
#to get just the domain of the website
try:
    link=url.split("https://")
    link1=link[1].split("/")
    link2=link1[0]
    link3=link2.split("www.")
    real_link=link3[1]

except IndexError:
    link=url.split("https://")
    link1=link[1].split("/")
    real_link=link1[0]

time.sleep(3)
screenshot=browser.save_screenshot("photos/"+"(" + real_link + ")" + ".png")
global content
content=[]
content.append(real_link)
content.append(description)
print(content)
browser.back()
time.sleep(5)
browser.execute_script("window.scrollBy(0,400)","")
time.sleep(5)

标签: pythonpython-3.xseleniumselenium-webdriverautomation

解决方案


您可以创建点击网站列表,并检查每次是否点击该链接。这是演示代码:

clicked_website=[]

url=browser.current_url
clicked_website.append(url)

# Now while clicking
if <new_url> not in clicked_website:
    <>.click()

这只是一个如何实施的想法。你的代码乱七八糟,我没看清楚,自己在你的代码中实现。


推荐阅读