Go to Blog: Shanghai-leisurely
Objective
Many small partners asked non-input tags how to upload documents, this is a hole in itself, helpless many small partners to jump in the pit, then introduce a non-mainstream upload file method, with third-party library SendKeys.
(This article is based on the python2.7 version, Python3 installation of SendKeys bag to find a way to do it yourself)
First, SendKeys installation
1.PIP mounting SendKeys
>pip Install SendKeys
2. At the time of installation if you appear above to save, do not rush the group to ask the Big God, the above has told the solution: Get it from http://aka.ms/vcpython27
3. Download the file according to the address given above, all the way to a fool-type installation on the line
4. The following interface appears, indicating that the installation was successful
Second, reference code
1. The following code is run through the Chrom browser, you must first log in to the blog Park to remember the password, and then load the configuration free login
2.chrome load Configuration method, just change the following place, username changed to your computer name (don't use Chinese!!!) )
'--user-data-dir=c:\users\username\appdata\local\google\chrome\user data '
3. Back two times the return, is because Sogou input method, the first return is to confirm the input, the second is to determine the selected file
# Coding:utf-8
From selenium import Webdriver
Import SendKeys
Import time
# Load Firefox configuration
# Profiledir = R ' C:\Users\xxxAppData\Roaming\Mozilla\Firefox\Profiles\1x41j9of.default '
# profile = Webdriver. Firefoxprofile (Profiledir)
# driver = Webdriver. Firefox (Profile)
# Load Chrome Configuration
option = Webdriver. Chromeoptions ()
Option.add_argument ('--user-data-dir=c:\users\xxxappdata\local\google\chrome\user data ')
Driver = Webdriver. Chrome (chrome_options=option)
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
Time.sleep (2)
Driver.find_element_by_class_name ("Qq-upload-button"). Click ()
# driver.find_element_by_name ("file"). Click ()
Time.sleep (5)
# SendKeys Method Input Content
Sendkeys.sendkeys ("D:\\test\\jie1\\blog\\12.png") # Send file Address
Time.sleep (1)
Sendkeys.sendkeys ("{ENTER}") # Send enter key
Time.sleep (1)
Sendkeys.sendkeys ("{ENTER}") # Because my computer is the search method, so look at the return
# Driver.quit ()
(Note: There is a hole in Firefox running here, the second return failed, this temporarily did not think of a good solution)
Selenium2+python Automation 75-Non-input file upload (SendKeys) "Reprint"