PS:Selenium 處理 js 中 alert、confirm、prompt三種彈窗javascript
HTML:html
<!DOCTYPE html>
<html>
<head>
<title>try alert</title>
</head>
<body>
</body>
<script type="text/javascript"> var result = alert("alert 例子"); </script>
</html>
代碼:java
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2019-07-15 16:12:33
# @Author : BenLam
# @Link : https://www.cnblogs.com/BenLam/
from selenium import webdriver
driver=webdriver.Firefox()
driver.get(r'alert.html')
alert = driver.switch_to.alert
print(alert.text)
# 確認
alert.accept()
# 取消
alert.dismiss()
driver.quit()
>>>alert 例子
>>>
HTML:python
<!DOCTYPE html>
<html>
<head>
<title>try confirm</title>
</head>
<body>
</body>
<script type="text/javascript"> var result = confirm("confirm 例子"); </script>
</html>
代碼:web
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2019-07-15 16:12:33
# @Author : BenLam
# @Link : https://www.cnblogs.com/BenLam/
from selenium import webdriver
driver=webdriver.Firefox()
driver.get(r'confirm.html')
confirm = driver.switch_to.alert
print(confirm.text)
# 確認
confirm.accept()
# 取消
confirm.dismiss()
driver.quit()
>>>confirm 例子
>>>
HTML:shell
<!DOCTYPE html>
<html>
<head>
<title>try prompt</title>
</head>
<body>
</body>
<script type="text/javascript"> var result = prompt("prompt 例子"); if (result !== null) { alert('您好~,' + result); } </script>
</html>
代碼:bash
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2019-07-15 16:12:33
# @Author : BenLam
# @Link : https://www.cnblogs.com/BenLam/
from selenium import webdriver
driver=webdriver.Firefox()
driver.get(r'prompt.html')
prompt = driver.switch_to.alert
print(prompt.text)
# prompt 特殊性是帶 text 文本輸入框
# 能夠用 send_keys 輸入內容
alert.send_keys("Test msg~")
# 確認
prompt.accept()
# 取消
prompt.dismiss()
driver.quit()
>>>prompt 例子
>>>