Frame 处理

时间:2024-04-01 13:05:38
 # -*- coding:utf-8 -*-
"""
通过 id 或 name 识别处理 fram 框架
""" from selenium import webdriver driver = webdriver.Firefox()
driver.get(r'F:\电子书\selenium\Selenium Testing Tools Cookbook(中文)\demo\frame\frame.html')
driver.switch_to_frame('left')
leftMsg = driver.find_element_by_tag_name('p').text
print leftMsg #回到初始的焦点
driver.switch_to_default_content() #通过 name 定位到右边的框架
driver.switch_to_frame('right') rightMsg = driver.find_element_by_tag_name('p').text print rightMsg
 # -*- coding:utf-8 -*-
"""
通过 index 焦点获取 frame 框架
""" from selenium import webdriver driver = webdriver.Firefox()
driver.get(r'F:\电子书\selenium\Selenium Testing Tools Cookbook(中文)\demo\frame\frame.html')
#通过 index 定位框架
#index 从 0 开始
driver.switch_to_frame(1) middleMsg = driver.find_element_by_tag_name('p').text print middleMsg
 # -*- coding:utf-8 -*-
"""
通过页面内容获取 frame 框架
""" from selenium import webdriver driver = webdriver.Firefox()
driver.get(r'F:\电子书\selenium\Selenium Testing Tools Cookbook(中文)\demo\frame\frame.html') #得到页面中所有的 frame 元素
frames = driver.find_elements_by_tag_name('frame') #通过页面内容得到页面中间的框架
for i in frames:
driver.switch_to_frame(i)
if "middle" in driver.page_source:
break
else:
#没有匹配到的时候回到最初页面
driver.switch_to_default_content() actualText = driver.find_element_by_tag_name('p').text
print actualText
print 'i am middle page' == actualText