特殊Dom结构是指对元素不能直接操作,需要进行特殊定位切换到它所在的Dom结构,然后才能对其元素进行操作
1、windows弹窗 --alert confirm prompt
使用driver.switch_to.alert方法
alert类操作方法:
- accept():确定
- dismiss():取消
- text():获取弹出框内的内容
- send_keys(keysToSend):输入字符串
# -*-coding:utf-8-*- from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time driver = webdriver.Chrome() driver.get("") driver.implicitly_wait(2) driver.find_element_by_id('window').find_element_by_tag_name('input').click() time.sleep(1) #等待弹窗出现 WebDriverWait(driver,20).until(EC.alert_is_present()) #切换进alert弹窗 alert = driver.switch_to.alert print(alert.text) alert.accept() time.sleep(3) driver.quit()
2、非windows弹窗
# -*-coding:utf-8-*- from selenium import webdriver import time driver = webdriver.Chrome() driver.get("") driver.implicitly_wait(10) driver.find_element_by_id('noWindow').find_element_by_tag_name('input').click() time.sleep(1) #关闭弹窗 driver.find_element_by_id('header-right').click() time.sleep(3) driver.quit()
3、frame与iframe
frame标签:frameset(不需要特殊处理)、frame、iframe
引导webdriver进入frame中:switch_to.frame(reference)
# -*-coding:utf-8-*- from selenium import webdriver import time driver = webdriver.Chrome() driver.get("") driver.implicitly_wait(10) #切换进iframe结构 driver.switch_to.frame('iframeContainer') time.sleep(1) driver.find_element_by_id('noWindow').find_element_by_tag_name('input').click() time.sleep(3) driver.quit()
- switch_to_default_content():退出frame结构
- switch_to.parent_frame():切换到上层的frame