Python自动化测试系列[v1.0.0][处理iframe]

如果页面存在iframe,那么我们是不能直接定位到iframe节点下的页面元素的,需要先切换到iframe里边去,然后再对iframe中的页面元素进行定位,而如果切换进iframe中后也是定位不到iframe外的元素的,还需要切换出去才能进行iframe外的元素的定位。
在经历过上前边多种操作的封装后,iframe的封装就简单了很多,接下来笔者将介绍封装后的方法以及如何调用。

方法封装

def switch_to_iframe(self, frame):
    """
    用于切换进页面的iframe控件
    :param iframe:
    :return:
    """
    self.driver.switch_to.frame(frame)
def switch_to_default_content(self):
    """
    从iframe中切换回主页页面
    :return:
    """
    self.driver.switch_to.default_content()

方法调用

def test_switch_iframe(self):  # 定义测试方法
    chrome_driver = webdriver.Chrome()
    chrome_driver.get("https://mail.163.com")
    time.sleep(10)
    frame = chrome_driver.find_element_by_xpath("//*[@id='loginDiv']/iframe")
    # 调用封装好的方法切换进iframe控件
    Browser_Controller(chrome_driver).switch_to_iframe(frame)  
    time.sleep(5)
    chrome_driver.find_element_by_name("email").send_keys("邮箱账号")
    chrome_driver.find_element_by_name("password").send_keys("邮箱密码")
    chrome_driver.find_element_by_id("dologin").click()

发布了231 篇原创文章 · 获赞 188 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/dawei_yang000000/article/details/105648554