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.
Selenium2+python Automation 33-File Upload (Send_keys)