为什么硒无法在启动箱模式弹出窗口中找到元素?

时间:2019-05-15 13:56:16

标签: python selenium automation webdriver

我正在尝试使用Selenium和Python在Windows 10上自动执行Webportal。在此过程中,我需要在启动模式对话框中单击“确定”按钮。我使用了所有可能的方法来查找元素。但是它仍然抛出无法找到元素的情况。

我尝试通过ID,xpath等查找元素。使用的时间也在等待。

<div class="popup-overlay fade bootbox-confirm in" aria-hidden="false"><div class="modal-backdrop fade in" style="height: 937px;"></div><div class="popup-outer"><div class="popup-container"><div class="popup-inner modal-body"><h4 class="yellow-txt popup-title"> Framework</h4><div class="framework-txt">Are you sure want to Complete this file ?</div></div><div class="modal-footer modelcenter"><button data-bb-handler="cancel" type="button" id="okbtn" class="btn btn-yellow">Cancel</button>&nbsp;<button data-bb-handler="confirm" type="button" id="okbtn" class="btn btn-yellow">OK</button>&nbsp;</div></div></div></div>

我的代码:

qccomplete=driver.find_element_by_id("btnQcComplete").click()
time.sleep(2)

driver.switch_to_alert()

time.sleep(2)
qccomplete_ok=driver.find_element_by_id("okbtn").click()

1 个答案:

答案 0 :(得分:0)

您尝试单击的元素是动态生成的。这就是为什么您需要在单击之前添加一个等待。尝试像这样单击以修改行(这样可能会丢失Sleep):

qccomplete=WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "btnQcComplete")).click()

qccomplete_ok = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "okbtn")).click()  

您需要在脚本顶部将其导入的位置:

from selenium.webdriver.support import expected_conditions as EC