Reputation: 33474
I need to accept an IE javascript alert using using the php webdriver for selenium This works for FF and Chrome, but IE fails
Here's the code I'm using:
$web_driver = new WebDriver();
// opens Internet explorer browser
$session = $web_driver->session('internet explorer');
// Navigates to page that has JS alert on close.
$session->open('http://mypage.com/');
// Closes Offer window
$session->deleteWindow();
// Accepts alert to leave page
$session->accept_alert(); // Except accept_alert isn't working correctly in IE
// Closes last window
$session->close();
// Kill session for garbage collection
unset($session);
I know there's this answer for Java and this answer for C#, but am looking for a PHP specific solution since the java methods aren't the same
Upvotes: 1
Views: 1909
Reputation: 153
This one took me forever to figure out. What I have done before is after the deleteWindow()
call, grab the newest window handle and set the focus to it. Once you have the alert in focus you can call accept_alert()
on it and the window will close as expected. Here's the idea.
$web_driver = new WebDriver();
$session = $web_driver->session('internet explorer');
$session->open('http://mypage.com/');
$session->deleteWindow();
// Here's the new part
$handles = $this->_session->window_handles(); // stores array of window handles
$new_handle = end($handles); // grabs the newest handle, in this case our alert
$this->_session->focusWindow($new_handle); // gives the alert window focus
// Should now work as expected
$session->accept_alert();
$session->close();
unset($session);
This will also work for the firefox and chrome drivers, so you can use the same code for all drivers if you wanted to. Good luck!
Upvotes: 1