我很想用 Python 处理警报。我想做的是:
- 打开一个网址
- 提交表格或点击一些链接
- 检查新页面是否有提示
我用 Javascript 使用 PhantomJS 制作了这个,但我会用 Python 制作。
这是javascript代码:
文件test.js:
var webPage = require('webpage');
var page = webPage.create();
var url = 'http://localhost:8001/index.html'
page.onConsoleMessage = function (msg) {
console.log(msg);
}
page.open(url, function (status) {
page.evaluate(function () {
document.getElementById('myButton').click()
});
page.onConsoleMessage = function (msg) {
console.log(msg);
}
page.onAlert = function (msg) {
console.log('ALERT: ' + msg);
};
setTimeout(function () {
page.evaluate(function () {
console.log(document.documentElement.innerHTML)
});
phantom.exit();
}, 1000);
});
文件index.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<form>
<input id="username" name="username" />
<button id="myButton" type="button" value="Page2">Go to Page2</button>
</form>
</body>
</html>
<script>
document.getElementById("myButton").onclick = function () {
location.href = "page2.html";
};
</script>
文件page2.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<meta charset="utf-8" />
</head>
<body onload="alert('hello')">
</body>
</html>
这行得通;它检测到 page2.html 上的警报。 现在我制作了这个 python 脚本:
测试.py
import requests
from test import BasicTest
from selenium import webdriver
from bs4 import BeautifulSoup
url = 'http://localhost:8001/index.html'
def main():
#browser = webdriver.Firefox()
browser = webdriver.PhantomJS()
browser.get(url)
html_source = browser.page_source
#browser.quit()
soup = BeautifulSoup(html_source, "html.parser")
soup.prettify()
request = requests.get('http://localhost:8001/page2.html')
print request.text
#Handle Alert
if __name__ == "__main__":
main();
现在,我如何使用 Python 检查 page2.html 上是否出现警报?首先我打开页面 index.html,然后是 page2.html。 我刚开始,所以任何建议将不胜感激。
附注 我还测试了 webdriver.Firefox() 但速度非常慢。 我也读了这个问题:Check if any alert exists using selenium with python
但它不起作用(下面是相同的先前脚本加上答案中建议的解决方案)。
.....
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
....
def main():
.....
#Handle Alert
try:
WebDriverWait(browser, 3).until(EC.alert_is_present(),
'Timed out waiting for PA creation ' +
'confirmation popup to appear.')
alert = browser.switch_to.alert()
alert.accept()
print "alert accepted"
except TimeoutException:
print "no alert"
if __name__ == "__main__":
main();
我得到错误:
"selenium.common.exceptions.WebDriverException: Message: Invalid Command Method.."
请您参考如下方法:
PhantomJS 使用 GhostDriver 来实现 WebDriver 有线协议(protocol),这就是它在 Selenium 中作为 headless 浏览器工作的方式。
很遗憾,GhostDriver 目前不支持警报。虽然看起来他们希望帮助实现这些功能:
https://github.com/detro/ghostdriver/issues/20
您可以切换到 PhantomJS 的 javascript 版本或在 Selenium 中使用 Firefox 驱动程序。
from selenium import webdriver
from selenium.common.exceptions import NoAlertPresentException
if __name__ == '__main__':
# Switch to this driver and switch_to_alert will fail.
# driver = webdriver.PhantomJS('<Path to Phantom>')
driver = webdriver.Firefox()
driver.set_window_size(1400, 1000)
driver.get('http://localhost:8001/page2.html')
try:
driver.switch_to.alert.accept()
print('Alarm! ALARM!')
except NoAlertPresentException:
print('*crickets*')
