selenium+python处理Alert弹窗

from selenium import  webdriver
import win32api
import win32con
from time import sleep
driver = webdriver.Ie()

import time
from selenium import webdriver


driver.maximize_window()
driver.implicitly_wait(6)
driver.get("https://www.baidu.com")
time.sleep(1)

driver.execute_script("window.alert('这是一个测试Alert弹窗');")
time.sleep(2)
driver.switch_to_alert().accept()  # 点击弹出里面的确定按钮

  

from selenium import  webdriver
import win32api
import win32con
from time import sleep
driver = webdriver.Ie()
driver.get('http://10.82.21.190')
driver.switch_to_alert().send_keys("admin")
win32api.keybd_event(9, 0, 0, 0)  # ctrl键位码是9
win32api.keybd_event(9, 0, win32con.KEYEVENTF_KEYUP, 0)  # 释放按键
win32api.keybd_event(65, 0, 0, 0)  # a键位码是65
win32api.keybd_event(65, 0, win32con.KEYEVENTF_KEYUP, 0)  # 释放按键
win32api.keybd_event(68, 0, 0, 0)  # d键位码是68
win32api.keybd_event(68, 0, win32con.KEYEVENTF_KEYUP, 0)  # 释放按键
win32api.keybd_event(77, 0, 0, 0)  # m键位码是77
win32api.keybd_event(77, 0, win32con.KEYEVENTF_KEYUP, 0)  # 释放按键
win32api.keybd_event(73, 0, 0, 0)  # i键位码是73
win32api.keybd_event(73, 0, win32con.KEYEVENTF_KEYUP, 0)  # 释放按键
win32api.keybd_event(78, 0, 0, 0)  # n键位码是78
win32api.keybd_event(78, 0, win32con.KEYEVENTF_KEYUP, 0)  # 释放按键
driver.switch_to_alert().accept()
sleep(3)
driver.find_element_by_css_selector("#cancelBtn").click()

  driver.switch_to_alert().accept()可以模拟点击确定,关闭弹窗按钮,这是一个老方法,新方法为driver.switch_to.alert().accept(),但是尝试新方法不能实现该功能,所以仍采用老方法。

 

posted @ 2019-04-12 16:46  autotest_man  阅读(924)  评论(0编辑  收藏  举报