selenmium - 操作弹出框
JS 弹出框
alert 弹框
html 代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Alert学习</title>
</head>
<body>
<h2>Alert Test</h2>
<script type="text/javascript">
function showAlert(){
alert(document.f1.t1.value);
}
</script>
<form name="f1">
<input type="text" name="t1" value="Alert Message"><br><br>
<input type="button" name="b1" value="Click For Alert" onclick="showAlert()"><br>
</form>
</body>
</html>
"""
selenium 操作alert弹出框
总结:
1. driver.switch_to.alert: 获得弹出框对象
2. driver.switch_to.alert.text: 获得文本信息
3. driver.switch_to.alert.accept(): 点击alert "确定"按钮
"""
import os
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
file = os.path.dirname(os.path.abspath(__file__))
file_path = file + os.sep + "myhtml6-9.html"
driver = webdriver.Chrome()
driver.get(file_path)
# 点击后弹出框
driver.find_element(By.NAME, "b1").click()
# 获得弹出框,注意是: switch_to.alert 没有括号
alert_el = driver.switch_to.alert
# 获得弹出框的值
print(alert_el.text)
sleep(1)
# 点击alert的确定按钮
alert_el.accept()
sleep(1)
driver.quit()
confirm 弹框
Prompt 弹框
非JS弹出框
本文来自博客园,作者:chuangzhou,转载请注明原文链接:https://www.cnblogs.com/czzz/p/15973401.html