Frame 处理
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').text 12 print leftMsg 13 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').text 21 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').text 16 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 break 19 else: 20 #没有匹配到的时候回到最初页面 21 driver.switch_to_default_content() 22 23 actualText = driver.find_element_by_tag_name('p').text 24 print actualText 25 print 'i am middle page' == actualText