Selenium positioning and switching frame (IFRAME)

Source: Internet
Author: User
Tags xpath

There are always people who can't understand, just in case, first capitalize in the beginning of the bold description:

frameset do not need to cut, frame need to cut layers.
A lot of people in the use of Selenium positioning page elements will encounter the problem of positioning, obviously the element is there, with Firebug can also be seen, is not located, this situation is likely to be frame in mischief (one of the reasons, another day specifically said the positioning of elements, Some of the possible causes and ways of handling).

Frame tag has frameset, frame, iframe three kinds, frameset with other ordinary label no difference, will not affect the normal positioning, and frame and IFRAME for selenium positioning is the same, Selenium has a set of methods to manipulate the frame.

1. How to cut into frame (switch_to.frame ())
Selenium provides the Switch_to.frame () method to toggle the frame

Switch_to.frame (Reference)

Have to mention Switch_to_frame (), a lot of people in this writing will find that this sentence is marked with a strikethrough, because this method has been out, and then very likely will not support, the proposed wording is switch_to.frame ()

Reference is an incoming parameter used to locate the frame, which can pass in the ID, name, index, and selenium webelement objects, assuming the following HTML code index.html:

 

To locate the IFRAME and cut it in, you can use the following code:

From selenium import webdriver
driver = webdriver. Firefox ()
driver.switch_to.frame (0)  # 1. Use frame's 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 the Webelement object to locate

Usually the ID and name are used to solve most problems. But sometimes frame does not have these two properties, you can use index and webelement to locate:

Index starting from 0, the incoming integer parameter is determined to use index positioning, the incoming str parameter is determined to use id/name positioning
Webelement object, which is the object obtained by using the Find_element series method, we can locate the frame object with Tag_name, XPath, etc.
Give me a chestnut:

<iframe src= "myframetest.html"/>

Use XPath to locate and pass in the Webelement object:

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

2. Cut back the main document from the frame (switch_to.default_content ())
After we cut into the frame, we can't continue to manipulate the elements of the main document, and if we want to manipulate the contents of the main document, we need to cut back to the main document.

Driver.switch_to.default_content ()

3. Operation of nested FRAME (Switch_to.parent_frame ())
Sometimes we encounter nested frame, as follows:

 

1. Cut from the main document to frame2 and cut in layers

Driver.switch_to.frame ("frame1")
driver.switch_to.frame ("frame2")

2. From frame2 back to frame1, here Selenium provides us with a way to cut back from the child frame to the parent frame without having to cut back into the main document.

Driver.switch_to.parent_frame ()  # No effect if it is currently a main document

With Parent_frame () this is the equivalent of a fallback method, we can switch to different frame at will, random jump to jump.

So as long as the use of the following three methods, encounter frame minutes to take care of:

Driver.switch_to.frame (Reference)
driver.switch_to.parent_frame ()
driver.switch_to.default_content ()

Add
In addition, I have seen before using Point division method to cut into nested frame method, but I found that after the experiment and can not locate the frame, if there are students to succeed, trouble message to inform, use as follows:

Driver.switch_to.frame (' Frame1.0.frame3 ')

It is said that the above code can be cut into "Frame3" under "Frame1" under "First frame".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.