Selenium2+python Automation 33-File Upload (Send_keys)

Source: Internet
Author: User

Objective

File uploads are a common feature on Web pages, and it's not as simple to automate success.

General Two scenarios: one is the input tag, this can be easily solved with the Send_keys () method provided by selenium;

Another non-input label can be difficult to implement, with the help of AutoIt tools or SendKeys third-party libraries.

This article takes the blog garden upload picture as the case, through the Send_keys () method solves the file upload question

First, identify the upload button

1. Click the Image upload button in the blog Park editor to pop up the "Upload local image" box.

2. Using Firebug to view button properties, this upload picture button has an obvious identifier, which is an input tag, and the value of the type attribute is file.

As soon as we find these two identities, we can upload the file directly using the Send_keys () method.

Second, locate the IFRAME

1. Position the image upload button here is a bit complicated, first of all it is on the IFRAME (do not understand the IFRAME see this: Selenium2+python Automation 14-iframe)

2. The ID of this IFRAME is dynamic and has no Name attribute, and other attributes are not obvious

3. Through the search, there are two iframe on this page, the IFRAME that needs to be located is in the second position

4. You can locate all the IFRAME tags by tag and then take the corresponding first few.

Third, File upload

1. Navigate to the File Upload button, directly call the Send_keys () method can be achieved

# Coding:utf-8
From selenium import Webdriver
Import time
Profiledir = R ' C:\Users\Gloria\AppData\Roaming\Mozilla\Firefox\Profiles\1x41j9of.default '
Profile = Webdriver. Firefoxprofile (Profiledir)
Driver = Webdriver. Firefox (Profile)
Driver.implicitly_wait (30)
Driver.get ("http://www.cnblogs.com/yoyoketang/")
Driver.find_element_by_link_text ("new essay"). Click ()
Time.sleep (3)
# Click to open Editor picture
Driver.find_element_by_css_selector ("Img.mceicon"). Click ()
Time.sleep (3)
# Locate all the IFRAME and take a second
iframe = Driver.find_elements_by_tag_name (' iframe ') [1]
# Switch to the IFRAME
Driver.switch_to_frame (IFRAME)

# File path
Driver.find_element_by_name (' file '). Send_keys (R "D:\test\xuexi\test\14.png")

File uploads that are not input tags are not available for this method and require the use of the AutoIt tool or the SendKeys third-party library.

The study process has encountered the question, may add selenium (Python+java) QQ Group Exchange: 232607095

Selenium2+python Automation 33-File Upload (Send_keys)

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.