我需要使用php webdriver for selenium接受IE javascript警报 这适用于FF和Chrome,但IE失败
这是我正在使用的代码:
$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);
我知道有this answer for Java和this answer for C#,但我正在寻找PHP特定的解决方案,因为java方法不一样
答案 0 :(得分:1)
这个让我永远想通了。我之前做的是在deleteWindow()
调用之后,抓住最新的窗口句柄并将焦点设置到它。一旦您获得了焦点警报,您可以在其上调用accept_alert()
,窗口将按预期关闭。这是想法。
$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);
这也适用于firefox和chrome驱动程序,因此如果您愿意,可以对所有驱动程序使用相同的代码。祝你好运!