Frame表单切换

WebDriver只能在一个页面上对元素识别和定位,对于frame/iframe表单内嵌页面上的元素无法直接定位,需要通过switch_to.frame()方法将当前定位的主题切换为frame/iframe表单的内嵌页面中。

switch_to.frame()方法默认可以直接取表单的id或name属性,如果没有可用的id或name,需要先通过xpath定位到iframe(),再将定位元素传到switch_to.frame()方法。

完成表单操作后,可以通过switch_to.parent_content()方法跳出当前表单,也可以通过switch_to.default_content()跳回最外层的页面。

 

 

例子:

"""
        将焦点转换到某个frame
        driver  浏览器驱动
        frameID frame的XPATH
        clickID 如果需要点击父页面的ID
"""
def go_to_frame_by_xpath(self, driver, frameID, clickID):
  driver.switch_to.default_content()
  if clickID != None:
    # 点击父页面的按钮,出现iframe
    WebDriverWait(driver, 30, 1).until(EC.presence_of_element_located((By.XPATH, clickID))).click()
   frameElement = WebDriverWait(driver, 30, 1).until(EC.presence_of_element_located((By.XPATH, frameID)))
   #将焦点转到iframe
  driver.switch_to.frame(frameElement)

 

调用:

frame().go_to_frame_by_xpath(driver, '//*[@id="ifrm_a219d027-57e2-4b95-a598-a1b600f8ab4d"]', None)

  

posted on 2018-03-16 20:55  瞎学瞎写  阅读(244)  评论(0编辑  收藏  举报