将以下代码copy至txt文本里,后缀改成html,然后用浏览器打开
<html>
<head>
<title>Alert</title>
</head>
<body>
<input id = "alert" value = "alert" type = "button" onclick = "alert('您关注了yoyoketang吗?');"/>
<input id = "confirm" value = "confirm" type = "button" onclick = "confirm('确定关注微信公众号:yoyoketang?');"/>
<input
id = "prompt" value = "prompt" type = "button" onclick = "var name =
prompt('请输入微信公众号:','yoyoketang'); document.write(name) "/>
</body>
</html>
如下图:
alert操作
1.先用switch_to_alert()方法切换到alert弹出框上
2.可以用text方法获取弹出的文本信息
3.accept()点击确认按钮
4.dismiss()相当于点右上角x,取消弹出框
from selenium import webdriver
import time
url = "file:///C:/User/admin/Desktop/text.html"
driver = webdriver.Chmore()
driver.get(url)
time.sleep(4)
driver.find_element_by_id("alert").click()
time.sleep(3)
t = driver.switch_to_alert()
print(t.text)
t.accept()
#t.dismiss()
confirm操作
1.先用switch_to_alert()方法切换到alert弹出框上
2.可以用text方法获取弹出的文本 信息
3.accept()点击确认按钮
4.dismiss()相当于点取消按钮或点右上角x,取消弹出框
from selenium import webdriver
import time
url = "file:///C:/User/admin/Desktop/text.html"
driver = webdriver.Chmore()
driver.get(url)
time.sleep(4)
driver.find_element_by_id("confirm").click()
time.sleep(3)
t = driver.switch_to_alert()
print(t.text)
t.accept()
#t.dismiss()
prompt操作
- 先用switch_to_alert()方法切换到alert弹出框上
- 可以用text方法获取弹出的文本信息
- accept()点击确认按钮
- dismiss()相当于点右上角x,取消弹出框
- send_keys()这里多个输入框,可以用send_keys()方法输入文本内容
from selenium import webdriver
import time
url = "file:///C:/User/admin/Desktop/text.html"
driver = webdriver.Chmore()
driver.get(url)
time.sleep(4)
driver.find_element_by_id("prompt").click()
time.sleep(3)
t = driver.switch_to_alert()
print(t.text)
t.send_keys("Hello world")
t.accept()
#t.dismiss()