Reputation: 455
I am trying to use Selenium WebDriver to click the 'OK' button in a modal that appears after submitting a form on a page.
Driver.SwitchTo().Alert()
fails, and so does switching the window handle. I have also tried Driver.SwitchTo().ActiveElement()
but that also fails. The driver is still recognising elements on the page before the button that opens the modal was clicked, so I know it is definitely not switching over, which makes Xpath and CssSelector useless for the meantime.
I have tried switching browsers but that doesn't have an effect.
<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>
Any suggestions would be helpful - thank you!
Upvotes: 0
Views: 2549
Reputation: 705
Clement's answer solved this for me. Except, I was using python, so what you need to do is invoke:
driver.implicitly_wait(1)
Or for more seconds if this doesn't work.
Upvotes: 0
Reputation: 675
I have solved a very similar problem to yours with the following code:
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(okButton_id));
IWebElement okButton = driver.FindElement(By.Id(okButton_id));
okButton.Click();
Upvotes: 1