Python3-Selenium自动化测试框架(六)之窗口切换、等待

Selenium自动化测试框架(六)之窗口切换、等待

窗口切换有三种:

  • 1、Windows切换
  • 2、iframe切换
  • 3、alert切换

一、Windows切换

获取所有窗口的句柄

handles = driver.window_handles

获取当前窗口的句柄

handle = driver.current_window_handle

通过所有窗口的句柄索引来进行窗口切换

driver.switch_to.window(driver.window_handles[-1])

窗口等待

等待新窗口(handles是新窗口出来之前所有的窗口句柄)
通过EC(expected_conditions)中的new_window_is_opened()方法进行等待。

WebDriverWait(driver, 30, 0.2).until(EC.new_window_is_opened(handles))

二、iframe切换

只有切换到iframe中,才可以定位到iframe中的元素。
iframe切换有三种方式:

  1. 通过name切换
  2. 通过webelement切换
  3. 通过iframe索引切换(索引从0开始)

1、通过name切换

driver.switch_to.frame('iframe的name')

2、通过webelement切换

# 先定位到iframe元素
iframe_element = driver.find_element_by_xpath("//iframe[@name='iframe的name']")
# 进行切换
driver.switch_to.frame(iframe_element)

3、通过iframe索引切换(索引从0开始)

driver.switch_to.frame(0)

切换到父级iframe

driver.switch_to.parent_frame()

切换到主页面

driver.switch_to.default_content()

iframe等待

通过EC(expected_conditions)中的frame_to_be_available_and_switch_to_it()方法进行等待。

WebDriverWait(driver, 30, 0.2).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@name='iframe的name']")))

三、alert切换

alert切换,返回的是一个Alert对象

mAlert = driver.switch_to.alert

点击确定按钮

mAlert.accept()

点击取消按钮

mAlert.dismiss()

alert等待

通过EC(expected_conditions)中的alert_is_present()方法进行等待。

WebDriverWait(driver, 10, 0.2).until(EC.alert_is_present())

【完】


猜你喜欢

转载自www.cnblogs.com/desireyang/p/12208426.html