Element not clickable since another element obscures it in python

时间:2018-07-25 05:05:50

标签: python selenium firefox ui-automation

I am trying to automate an access point web configuration. During this, I get a pop up (kind of an overlay with "Yes" and "No") which i want to click on

The HTML code for the overlay that I am trying to click on:

<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> 

I tried

browser.find_element_by_class_name("submit").click()

but I get the following error:

raise exception_class(message, screen, stacktrace) selenium.common.exceptions.ElementClickInterceptedException: Message: Element is not clickable at point (788,636.5) because another element obscures it

Can you please advise on how should i proceed? I am using firefox and python

3 个答案:

答案 0 :(得分:2)

根据您的问题和共享的 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()

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

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

答案 1 :(得分:0)

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

使用操作类:

from selenium.webdriver.common.action_chains import ActionChains

actions = ActionChains(driver)
actions.move_to_element("Your Web Element").click().perform()

答案 2 :(得分:0)

您尝试单击一个弹出窗口,当硒尝试单击该元素时,它可能在DOM中可用,但不可见,然后您会收到此错误。 / p>

解决方案是在单击之前等待该元素可单击/可见。这是用于此的C#代码(您可以用Python编写等效代码):

WaitForElementToBeClickable(By.ClassName("submit"), 20);
browser.FindElement(By.ClassName("submit")).click();


public static void WaitForElementToBeClickable(By by, int timeout)
    {
        new WebDriverWait(Driver, TimeSpan.FromSeconds(timeout)).Until(ExpectedConditions.ElementToBeClickable(by));
    }