Element not clickable since another element obscures it in python

放肆的年华 提交于 2021-01-27 14:45:52

问题


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


回答1:


As per your question and the HTML you have shared, you need to induce WebDriverWait for the desired element to be clickable and you can use the following solution:

#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()

Note : You have to add the following imports :

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



回答2:


You can Replace click event with action class

Using Action Class :

from selenium.webdriver.common.action_chains import ActionChains

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


来源:https://stackoverflow.com/questions/51511142/element-not-clickable-since-another-element-obscures-it-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!