UI自动化之下拉框、窗口切换、弹窗处理
UI自动化过程经常会碰到下拉框处理,若下拉框使用的是select标签,则可使用Select类进行处理。其他div标签的下来框处理则使用切换到下来按钮点击后再点击对应内容进行处理。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>脚本语言</title> </head> <body> <center> <select id="yy" name="yy"> <option value="python">python语言</option> <option value="java">java语言</option> <option value="ruby">ruby语言</option> </select> </center> </body> </html>
from selenium import webdriver from selenium.webdriver.support.select import Select import time as t driver = webdriver.Chrome() driver.maximize_window() driver.get("D:\\pycharmwokspace\\UITEST\\html\\index2.html") el = driver.find_element_by_id("yy") Select(el).select_by_index(0) #通过索引选择 t.sleep(2) Select(el).select_by_visible_text("ruby语言") #通过下拉文本值选择 t.sleep(2) Select(el).select_by_value("python") #通过option value值选择 t.sleep(2) driver.quit()
UI自动化过程经常会碰到多个浏览页签进行切换处理,可使用switch_to_window()处理
from selenium import webdriver from selenium.webdriver.support.select import Select import time as t driver = webdriver.Chrome() driver.maximize_window() driver.get("D:\\pycharmwokspace\\UITEST\\html\\index2.html") cu_window = driver.current_window_handle #获取当前窗口句柄 all_windwos = driver.window_handles #获取所有打开窗口句柄 for handle in all_windwos: #循环窗口句柄 if handle != cu_window: #不是当前窗口句柄 driver.switch_to_window(handle) #切换到新窗口 driver.maximize_window()
UI自动化过程经常会碰到alter弹窗处理,可使用Alter处理
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>测试</title> </head> <body> <script> alert("自动化测试") </script> </body> </html>
from selenium import webdriver from selenium.webdriver.support.select import Select import time as t from selenium.webdriver.common.alert import Alert driver = webdriver.Chrome() driver.maximize_window() driver.get("D:\\pycharmwokspace\\UITEST\\html\\index3.html") print(driver.switch_to.alert.text) t.sleep(2) driver.switch_to.alert.accept() driver.quit()