元素不可点击,因为另一个元素在 python 中遮住了它

Klo*_*lot 3 python firefox selenium ui-automation

我正在尝试自动化接入点 Web 配置。在此期间,我会弹出一个窗口(类似于“是”和“否”的叠加层),我想点击它

我试图点击的叠加层的 HTML 代码:

<div id="generic-warning-dialog" class="dialog exclamation text-orphan" style="">
<div class="warning-content dialog-content text-orphan">Security Mode is disabled on one or more of your wireless networks. Your network could be open to unauthorized users. Are you sure you wish&nbsp;to&nbsp;proceed?</div>
    <div class="dialog-buttons text-orphan">
        <button class="cancel">No</button>
        <button class="submit" style="display: block;">Yes</button>
    </div>
</div> 
Run Code Online (Sandbox Code Playgroud)

我试过

browser.find_element_by_class_name("submit").click()
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

引发 exception_class(message, screen, stacktrace) selenium.common.exceptions.ElementClickInterceptedException:消息:元素在点 (788,636.5) 处不可点击,因为另一个元素遮住了它

你能告诉我应该如何进行吗?我正在使用火狐和蟒蛇

Ish*_*hah 5

您可以用操作类替换单击事件

使用动作类:

from selenium.webdriver.common.action_chains import ActionChains

actions = ActionChains(driver)
actions.move_to_element("Your Web Element").click().perform()
Run Code Online (Sandbox Code Playgroud)


Deb*_*anB 5

根据您的问题和您共享的HTML ,您需要引发WebDriverWait以使所需元素可单击,您可以使用以下解决方案:

#to click on Yes button
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='warning-content dialog-content text-orphan' and contains(.,'Security Mode is disabled')]//following::div[1]//button[@class='submit']"))).click()
# to click on No button
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='warning-content dialog-content text-orphan' and contains(.,'Security Mode is disabled')]//following::div[1]//button[@class='cancel']"))).click()
Run Code Online (Sandbox Code Playgroud)

注意:您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Run Code Online (Sandbox Code Playgroud)

  • 这对我不起作用。似乎按钮已经是“可点击的”,但点击它仍然会导致“模糊错误” - 在我的情况下,它是基础 Reveal/modal 中的按钮(请参阅 https://foundation.zurb.com/sites/docs/reveal .html) 有什么想法吗? (5认同)