1 # -*- coding:utf-8 -*- 2 """ 3 通过 id 或 name 识别处理 fram 框架 4 """ 5 6 from selenium import webdriver 7 8 driver = webdriver.Firefox() 9 driver.get(r'F:\电子书\selenium\Selenium Testing Tools Cookbook(中文)\demo\frame\frame.html')10 driver.switch_to_frame('left')11 leftMsg = driver.find_element_by_tag_name('p').text12 print leftMsg13 14 #回到初始的焦点15 driver.switch_to_default_content()16 17 #通过 name 定位到右边的框架18 driver.switch_to_frame('right')19 20 rightMsg = driver.find_element_by_tag_name('p').text21 22 print rightMsg
1 # -*- coding:utf-8 -*- 2 """ 3 通过 index 焦点获取 frame 框架 4 """ 5 6 7 from selenium import webdriver 8 9 driver = webdriver.Firefox()10 driver.get(r'F:\电子书\selenium\Selenium Testing Tools Cookbook(中文)\demo\frame\frame.html')11 #通过 index 定位框架12 #index 从 0 开始13 driver.switch_to_frame(1)14 15 middleMsg = driver.find_element_by_tag_name('p').text16 17 print middleMsg
1 # -*- coding:utf-8 -*- 2 """ 3 通过页面内容获取 frame 框架 4 """ 5 6 from selenium import webdriver 7 8 driver = webdriver.Firefox() 9 driver.get(r'F:\电子书\selenium\Selenium Testing Tools Cookbook(中文)\demo\frame\frame.html')10 11 #得到页面中所有的 frame 元素12 frames = driver.find_elements_by_tag_name('frame')13 14 #通过页面内容得到页面中间的框架15 for i in frames:16 driver.switch_to_frame(i)17 if "middle" in driver.page_source:18 break19 else:20 #没有匹配到的时候回到最初页面21 driver.switch_to_default_content()22 23 actualText = driver.find_element_by_tag_name('p').text24 print actualText25 print 'i am middle page' == actualText