Selenium - web page frame and multi-window processing!

1. Multi-window processing.

1.1. Introduction to multi-window

Clicking some links will reopen a window. In this case, if you want to operate on a new page, you
must switch windows first.
The unique identifier of the obtained window is represented by a handle, so you only need to switch the handle and you can operate on multiple pages
>

1.2. Multi-window processing flow

  • First get the handle of the current window driver.current_window_handle
  • Then get all window handles driver.windows_handles
  • Then determine whether the current window is the window that requires operation. If not, switch to the next window. If so, perform the operation in the current window.
def test_switch_window(self):
        """窗口切换操作"""
        # 1、打开百度
        self.driver.get("https://www.baidu.com")
        print(self.driver.title, self.driver.current_window_handle)
        # 2、打开搜狗
        self.driver.switch_to.new_window()
        self.driver.get("https://www.sougou.com")
        print(self.driver.title, self.driver.current_window_handle)
        # 3、打开hao360
        self.driver.switch_to.new_window()
        self.driver.get("https://hao.360.com/")
        print(self.driver.title, self.driver.current_window_handle)
        # 4、打开测试人
        self.driver.switch_to.new_window()
        self.driver.get("https://ceshiren.com")
        print(self.driver.title, self.driver.current_window_handle)
        handles = self.driver.window_handles
        print(handles)
        self.driver.switch_to.window(handles[0])
        self.driver.switch_to.window(handles[-1])
        print(self.driver.title)

2. Multiple web page frame processing

2.1. Introduction to frame

In web automation, if an element cannot be positioned, it is most likely that it is in the frame.
  • What is a frame?
Frame is the frame of HTML. The so-called frame means that more than one area can be displayed on the same page. Based on the HTML frame, it can be divided into vertical frame and horizontal frame (cols, rows)

 frame classification
The frame tag is divided into three types: frameset, ifame and frame
Frameset is the same as ordinary tags and will not affect normal element positioning , you can use index, id, name, webelement and other methods to locate frame
frame and iframe are equivalent to selenium, and you need to perform some special operations before you can locate the element driver.switch_to.frame( "child node") driver.switch_to.frame("parent node") For a nested frame, first enter the parent node of the frame, and then enter the child node. node, and then you can operate on the element objects in the child nodes Nested frame driver.switch_to.frame('frame-index'): When there is no id Processed according to the index, the index starts from 0 driver.switch_to.frame('frame's id'): When there is an id, the id is used first Unnested frame deiver.switch_to.parent_frame(): Switch to the parent frame driver.switch_to .default_content(): Switch to the default frame driver.swich_to.frame(): Switch frame according to element id and index Switch frame One is not Nested One is nested There are two types of frames
2.2. Multi-frame switching













def test_switch_frame(self):
        # ❖ 打开包含frame的web页⾯ https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable
        # ❖ 打印’请拖拽我’元素的⽂本
        # ❖ 打印’点击运⾏’元素的⽂
        self.driver.get("https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable")
        self.driver.switch_to.frame("iframeResult")
        ele01 = self.driver.find_element(By.ID, "draggable")
        print(ele01.text)
        ele02 = self.driver.find_element(By.ID, "droppable")
        print(ele02.text)
        self.action.drag_and_drop(ele01, ele02).perform()
        time.sleep(3)
        self.driver.switch_to.alert.accept()
        # self.driver.switch_to.default()
        # self.driver.switch_to.parent_frame()
Summarize:

Thank you to everyone who reads my article carefully! ! !

As someone who has experienced it, I also hope that everyone will avoid some detours. If you don’t want to experience the feeling of not being able to find information, no one to answer questions, and giving up after persisting for a few days while studying, here I will share with you some learning about automated testing. Resources, I hope they can help you on your way forward

 

Software Testing Interview Document

We must study to find a high-paying job. The following interview questions are from the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.

 

Guess you like

Origin blog.csdn.net/2301_79535733/article/details/135019962