问题
I am using Firefox 11 + WebDriver 2.21.0 / WebDriver 2.22.0 (tried both).
In my scenario, when I click on a tab, it opens a confirmation box and on clicking OK
it starts loading the new tab from server.
So I'm handling this scenario as:
driver.findElement(By.id("myTab")).click();
driver.switchTo().alert().accept();
but after it clicks on "mytab", it waits for window to load indefinitely. So it is not coming on alert.accept()
and browser waits to accept the confirmation dialog to load the new page, so I end up in a deadlock condition.
This code works well in Internet Explorer.
Please help, how to deal the situation?
回答1:
You, sir, might have found a bug (or at least an inconsistency) in Selenium WebDriver.
Look here whether it's been found before, and if there's no such bug, feel free to file it.
In the meantime, you can try loading FirefoxDriver with "unstable" loading strategy and then (if it's not enough) possibly driver.manage().timeouts().pageLoadTimeout() (which works only for Firefox with the "unstable" setting).
As a workaround, you can try clicking the tab via JavaScript - though I'm not sure whether it will or won't work:
((JavascriptExecutor)driver).executeScript("document.getElementById('myTab').click()");
Edit:
What you could do as another workaround (inspired by Selenium RC), you can temporarily disable confirmation dialogs...
// assuming your driver can handle JS ;)
JavascriptExecutor js = (JavascriptExecutor)driver;
// stores the original confirm() function and replaces it
js.executeScript("window.originalConfirm = window.confirm;"
+ "window.confirm = function(m) { return true; };");
driver.findElement(By.id("myTab")).click();
// it should not even fire the confirm and just proceed
// get the confirm back
js.executeScript("window.confirm = window.originalConfirm;");
来源:https://stackoverflow.com/questions/10947857/handling-alert-in-webdriver-2-21and-mozilla11