问题: GhostDriver API尚不支持警报处理.目前有一个可接受的解决方法,即将自己的javascript注入到处理警报并为您存储文本的页面中.
我通过python webdriver绑定使用此解决方法时遇到问题.这可能与我对新手级别的javascript理解有关.
以下是我尝试使用的解决方法示例:https: //github.com/detro/ghostdriver/issues/20#issuecomment-15641983
我正在使用一个显示警报的公共站点,以使其更简单:http://www.tizag.com/javascriptT/javascriptalert.php
这是我的代码:
from selenium import webdriver
button_xpath = "/html/body/table[3]/tbody/tr/td[2]/table/tbody/tr/td/div[4]/form/input"
js = """
(function () {
var lastAlert = undefined;
window.alert = function (message) {
lastAlert = message;
};
window.getLastAlert = function () {
var result = lastAlert;
lastAlert = undefined;
return result;
};
}());
"""
driver = webdriver.PhantomJS()
driver.get('http://www.tizag.com/javascriptT/javascriptalert.php')
driver.execute_script("window.alert = %s" % js)
driver.find_element_by_xpath(button_xpath).click()
#exception just occured
driver.execute_script("return window.getLastAlert && window.getLastAlert();")
Run Code Online (Sandbox Code Playgroud)
例外是:
WebDriverException: Message: u'Error Message => …Run Code Online (Sandbox Code Playgroud)