Cut frame

There are always people who don’t understand, just in case, first capitalize and boldly explain at the beginning:


Frameset does not need to be cut, frame needs to be cut layer by layer!

Many people encounter the problem of not being able to locate the page elements when using selenium. The element is clearly there, and you can see it with firebug, but it is not positioned. In this case, it is very likely that the frame is messing up (the reason 1. Another day, I will specifically talk about some possible reasons and solutions for the inability to locate the element).


There are three types of frame tags: frameset, frame, and iframe. Frameset is no different from other ordinary tags and will not affect normal positioning. Frame and iframe are the same for selenium positioning. Selenium has a set of methods to operate on the frame.


1. How to switch to the frame (switch_to.frame())

Selenium provides switch_to.frame() method to switch frame


switch_to.frame(reference)

1

I have to mention switch_to_frame(). Many people will find that this sentence is struck through when writing this way. The reason is that this method is out, and it may not be supported later. The recommended way of writing is switch_to. frame()


Reference is the parameter passed in, used to locate the frame. You can pass in id, name, index, and selenium WebElement objects. Suppose there is the following HTML code index.html:


<html>

<head>

    <title>FrameTest</title>

</head>

<body>

<iframe src="a.html" id="frame1" name="myframe"></iframe>

</body>

</html>

1

2

3

4

5

6

7

8

To locate the iframe and cut into it, you can use the following code:


from selenium import webdriver

driver = webdriver.Firefox()

driver.switch_to.frame(0) # 1. Use frame index to locate, the first one is 0

# driver.switch_to.frame("frame1") # 2. Use id to locate

# driver.switch_to.frame("myframe") # 3. Use name to locate

# driver.switch_to.frame(driver.find_element_by_tag_name("iframe")) # 4. Use WebElement object to locate

1

2

3

4

5

6

Usually id and name can solve most problems. But sometimes frame does not have these two attributes, you can use index and WebElement to locate:


The index starts from 0. When the integer parameter is passed in, it is judged to be positioned by index, and the str parameter is passed in to judge to be positioned by id/name

WebElement object, that is, the object obtained by the find_element series of methods, we can use tag_name, xpath, etc. to locate the frame object

Give a chestnut:


<iframe src="myframetest.html" />

1

Use xpath to locate and pass in the WebElement object:


driver.switch_to.frame(driver.find_element_by_xpath("//iframe[contains(@src,'myframe')]"))

1

2. Switch back to the main document from the frame (switch_to.default_content())

After switching to the frame, we can no longer continue to manipulate the elements of the main document. At this time, if we want to manipulate the content of the main document, we need to switch back to the main document.


driver.switch_to.default_content()

1

3. Operation of nested frame (switch_to.parent_frame())

Sometimes we will encounter nested frames, as follows:


<html>

    <iframe id="frame1">

        <iframe id="frame2" / >

    </iframe>

</html>

1

2

3

4

5

1. Cut from the main document to frame2, cut in layer by layer


driver.switch_to.frame("frame1")

driver.switch_to.frame("frame2")

1

2

2. Switch back to frame1 from frame2, here selenium provides us with a method to switch from the child frame back to the parent frame, without us having to switch back to the main document and then cut in.


driver.switch_to.parent_frame() # If it is currently the main document, it has no effect

1

With parent_frame(), which is equivalent to the backward method, we can switch between different frames at will, jumping around at will.


So as long as you make good use of the following three methods, you can get it in minutes when you encounter a frame:


driver.switch_to.frame(reference)

driver.switch_to.parent_frame()

driver.switch_to.default_content()

1

2

3

supplement

In addition, I have seen the method of using the dot method to cut into the nested frame before, but I found that the frame cannot be located after the experiment. If some students can succeed, please leave a message and let me know. The usage is as follows:


driver.switch_to.frame('frame1.0.frame3')

1

It is said that the above code can be cut to "frame3" under "first frame" under "frame1".


Guess you like

Origin blog.51cto.com/14492955/2664443
cut