Python Automated Test Series [v1.0.0] [Processing iframe]

If there is an iframe on the page, then we cannot directly locate the page element under the iframe node. We need to switch to the iframe first, and then position the page element in the iframe. If we switch to the iframe, we cannot locate it. For elements outside the iframe, you need to switch out to locate the elements outside the iframe.
After going through the encapsulation of various operations above, the encapsulation of iframe is much simpler. Next, the author will introduce the encapsulated method and how to call it.

Method encapsulation

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()

Method call

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()

Published 231 original articles · praised 188 · 120,000 views

Guess you like

Origin blog.csdn.net/dawei_yang000000/article/details/105648554