如何使用Python处理警报?
我喜欢用Python处理警报。我喜欢做的是:如何使用Python处理警报?,python,selenium,selenium-webdriver,phantomjs,alert,Python,Selenium,Selenium Webdriver,Phantomjs,Alert,我喜欢用Python处理警报。我喜欢做的是: 打开url 提交表单或单击某些链接 检查新页面中是否出现警报 我用Javascript和PhantomJS实现了这一点,但我甚至可以用Python实现 以下是javascript代码: 文件test.js: var webPage = require('webpage'); var page = webPage.create(); var url = 'http://localhost:8001/index.html' page.onCons
- 打开url
- 提交表单或单击某些链接
- 检查新页面中是否出现警报
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>
现在,如何使用Python检查page2.html上是否出现警报?首先打开index.html页面,然后打开page2.html页面。
我才刚开始,所以任何建议都将不胜感激
p、 美国。
我还测试了webdriver.Firefox(),但速度非常慢。
我还读到这个问题:
但它不起作用(下面是与前面相同的脚本加上答案中建议的解决方案)
我得到一个错误:
“selenium.common.exceptions.WebDriverException:消息:无效
命令方法..”
PhantomJS使用GhostDriver实现WebDriver Wire协议,这就是它在Selenium中作为无头浏览器的工作方式 不幸的是,GhostDriver目前不支持警报。尽管看起来他们希望帮助实现这些功能: 您可以切换到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*')
从selenium导入webdriver
从selenium.common.exceptions导入NoAlertPresentException
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
#切换到此驱动程序和切换到警报将失败。
#driver=webdriver.PhantomJS(“”)
driver=webdriver.Firefox()
驱动程序。设置窗口大小(1400,1000)
司机,上车http://localhost:8001/page2.html')
尝试:
driver.switch_to.alert.accept()
打印('Alarm!Alarm!')
除NoAlertPresentException外:
打印(“*蟋蟀*”)
嗨!谢谢你的回答。我试图运行你的解决方案,但它不起作用。它打开一个空白页,什么也没发生。我遗漏了什么吗?脚本使用Python 3.5解释器运行,这就是您使用的吗?我建议将“main”下的代码和import语句复制并粘贴到您自己的脚本中。如果运行正常,它应该会打开一个firefox窗口指向您的URL,并将两条消息中的一条打印到控制台。我切换到Python3.5,但结果是一样的。我也采纳了你的建议。我使用的是selenium v.2.53.6,当所有其他操作都失败时,调试:)打开python控制台并逐行执行。(跳过if name=='main':行)。我认为这是Python没有看到webdriver或webdriver无法连接到URL的问题。脚本真正做的就是打开URL,尝试切换到警报,然后打印到控制台
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();
.....
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();
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*')