"Selenium2 Python Automation Test" (8)--positioning IFRAME

Source: Internet
Author: User

Let's take a look at the earliest code:

# Coding:utf-8

From seleniumImport Webdriver
From timeImport Sleepdriver = Webdriver. Firefox () Driver.get ("http://blog.hexun.com/") driver.find_element_by_id ("Read_btn"). Click () Driver.maximize_window () Sleep (3)
DRIVER.FIND_ELEMENT_BY_ID ("username"). Send_keys ("Account") driver.find_element_by_id ("Password"). Send_keys ("Password") Driver.find_element_by_xpath (".//*[@id = ' login12 ']/div/div/div/div[3]/input"). Click () Sleep (3)
# Click to send a blog
Driver.find_element_by_xpath (".//*[@id = ' login12 ']/div/div/div[3]/a[1]"). Click () Sleep (3) Curr = Driver.current_window_handleall_curr = Driver.window_handles
For IIn All_curr:
If I! = Curr:driver.switch_to.window (i) Sleep (2) driver.find_element_by_id ("Hxjy_blog_tit"). Send_keys ( U "My Posts")
        driver.switch_to.frame (Driver.find_element_by_xpath (".//*[@id = ' Editorcontainer ']/iframe ")        sleep ( 2)
        Driver.find_element_by_xpath ( "Html/body"). Click ()        driver.find_element_by_xpath ( "Html/body"). Send_keys ( u "Donglin")         driver.switch_to.default_content (
        # page drop-down        JS = "var q=document.document element.scrolltop=1000 "       driver.execute_script (JS)        driver.find_ Element_by_xpath ( ".//*[@id = ' Hxjy_blog_label ']"). Send_keys ( u "test")         Driver.find_element_by_xpath ( ".//*[@id = ' postarticle ']"). Click ()
       

This is the code I gave you when I first talked about selenium, so I'll take this code for example:

1, how to identify is not an IFRAME

When we click on the element that we want to see with the Arrow tool, under the Firepath tab, there is a hint of "iframe.editor" or "iframe" at the location shown in the box, and we can determine--this is an IFRAME.

2. Locating the IFRAME

This I feel a little trouble, do not know how to position the great God, I say my ideas, we can refer to. Because a little edit box into the IFRAME, so I do not know how to locate the location of the IFRAME, so I chose to locate the iframe above the element, that is, the positioning of the box there, and then see the following elements, click the div tag, find the IFRAME inside, Conveniently, when the mouse moves to the label, the page will display the corresponding location:

We see the IFRAME, then put the mouse over the IFRAME and right-click the copy XPath:

This allows us to navigate to the location of the IFRAME.

Positioning to the next, we want to transfer to the IFRAME, Pycharm hint method is Switch_to_frame (), but this method is outdated, if you use this method, Pycharm will underline you, the latest method is Switch_to.frame ( ), the pass parameter is the location of the IFRAME is the code in the black sentence:

driver.switch_to.frame(driver.find_element_by_xpath(".//*[@id=‘editorContainer‘]/iframe"))

Now we are in the IFRAME, then the normal positioning, input content is ok (see Code)

3, how to jump out of the IFRAME

After entering the IFRAME it is like entering a box where something can be found, but the outside of the box cannot be found. So when we're done, and we continue to locate the top window in the Find box, it proves it's not an IFRAME, we need to jump out of the box.

A word can jump out, like some people and you say "I have put down" just leave you, haha, you can be so dashing to the people who do not love you say.

is also a sentence, is the top code in the Black italic word:

driver.switch_to.default_content()

This will jump out of the IFRAME. A lot of login box, the article input box is an IFRAME write, now for you should be not difficult, everyone try it ~ ~

If you have any questions, you can leave a message

Public search "automated test Practice" or scan the QR code below to add attention ~ ~ ~

"Selenium2 Python Automation Test" (8)--positioning IFRAME

Related Article

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.