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, '//*[@]', None)

  

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-23
  • 2022-01-22
  • 2022-12-23
  • 2021-05-20
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-01
相关资源
相似解决方案