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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章