无法使用Selenium WebDriver在javascript模式中单击元素

无法使用Selenium WebDriver在javascript模式中单击元素,javascript,selenium-webdriver,modal-dialog,Javascript,Selenium Webdriver,Modal Dialog,我试图使用SeleniumWebDriver在页面上提交表单后出现的模式中单击“确定”按钮 Driver.SwitchTo().Alert()失败,切换车窗把手也会失败。我也尝试了Driver.SwitchTo().ActiveElement(),但也失败了。在单击打开模式的按钮之前,驱动程序仍在识别页面上的元素,因此我知道它肯定不会切换,这使得Xpath和CssSelector暂时无用 我尝试过切换浏览器,但没有效果 <div id="confirmModal" class="modal

我试图使用SeleniumWebDriver在页面上提交表单后出现的模式中单击“确定”按钮

Driver.SwitchTo().Alert()
失败,切换车窗把手也会失败。我也尝试了
Driver.SwitchTo().ActiveElement()
,但也失败了。在单击打开模式的按钮之前,驱动程序仍在识别页面上的元素,因此我知道它肯定不会切换,这使得Xpath和CssSelector暂时无用

我尝试过切换浏览器,但没有效果

<div id="confirmModal" class="modal fade">
<div class="modal header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
<h3 id="myModalLabel">Confirmation</h3>
</div>
<div class="modal-body">
<p>Confirm?</p>
<div class="alert alert-block">
<p> Are you sure? You can not undo this </p>
</div>
</div>
<div class="modal-footer">
<input type="button" class="btw" data-dismiss="modal" value="Cancel" aria-hidden="true">
<input type="button" value="OK" id="confirmation-submit" class="btn btw-primary">
</div>

</div>

x
确认书
确认

你确定吗?你不能撤销这个


任何建议都会很有帮助-谢谢

我用以下代码解决了一个与您非常类似的问题:

new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(okButton_id));
IWebElement okButton = driver.FindElement(By.Id(okButton_id));
okButton.Click();

克莱门特的回答为我解决了这个问题。除了,我使用的是python,所以您需要做的是调用:

driver.implicitly_wait(1)
或者,如果这不起作用的话,再等几秒钟