Selenium是一种用于自动化测试的工具,它可以模拟用户在网页上的操作和行为。当遇到弹窗时,可以使用Selenium的Alert类来处理。
代码实现:
alert.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
alert('这个是测试弹窗内容')
</script>
</body>
</html>
main.py
from selenium import webdriver
import os
from time import sleep
def testalert():
driver = webdriver.Chrome()
file_path = 'file:///'+os.path.abspath('./1/html/alert.html')
driver.get(file_path)
sleep(3)
driver.switch_to_alert().accept()
sleep(3)
driver.quit()
if __name__=="__main__":
testalert()