Python uploads files from Baidu cloud,
Environment preparation
Python3.6
PyCharm 2017.1.3
Windows Environment
Framework Construction
Selenium3.6
Installation Method:
Pip install selenium
Steps:
1. Step Analysis
1. Select "account and password Logon"
2. Enter the user name and password, and log on
3. File Upload
Note: This article describes how to use the webdriver In the selenium package to load the Firefox browser.
Ii. Element capturing
Use firebug plug-in of Firefox browser to copy the control's XPATH path. Note: Python3.6 corresponds to Firefox version 40.x, and does not support the latest version 50.x.
1. Click "account and password Logon" to obtain the source file.
As follows:
Right-click and copy the Xpath path:/html/body/div [1]/div [3]/div [6]/div [6]/div [2]/
The login button and file upload are the same as above to obtain the corresponding Xpath path.
Code:
1 # select the account and password to log on to 2 driver. find_element_by_xpath ('/html/body/div [1]/div [3]/div [6]/div [6]/div [2]/'). click () 3 # log on to 4 driver. find_element_by_xpath ('/html/body/div [1]/div [3]/div [6]/div [3]/form/p [5]/input '). send_keys ('username') 5 driver. find_element_by_xpath ('/html/body/div [1]/div [3]/div [6]/div [3]/form/p [6]/input '). send_keys ('Password') 6 driver. find_element_by_xpath ('/html/body/div [1]/div [3]/div [6]/div [3]/form/p [9]/input '). click ()
2. After logging on, Click Upload. The file dialog box is displayed.
The path to "Upload" is // * [@ id = "h5Input0"]
Code:
1 # upload 2 driver. find_element_by_xpath ('// * [@ id = "h5Input0"]'). click ()
Click Upload. The file dialog box is displayed.
Iii. Compile scripts to upload files
Webdriver cannot perform operations on files directly. Therefore, you need to use AutoIT to upload files.
AutoIT: https://www.autoitscript.com/site
After installing AutoIt, open AutoIt Window Info (x64)
4. Obtain the control information of the file upload window:
Open the autoit Tool and drag the Finder Tool icon to the control to be recognized.
5. Compile an AutoIt script to upload files.
3. Save the file upfile. au3
4. Use compile script to exe to compile the above AutoIt script into an exe file for the python script to call
6. Finally, use the Python script to call the AutoIT script.
1 # Click Upload to open the Upload File window. 2 driver. find_element_by_xpath ('/html/body/div [1]/div [2]/div [1]/div [2]/div [2]/div [2]/[ 1]/form/input '). click () 3 4 # Use the autoit script to automatically upload files 5 # import the python OS library file: import os6 OS. system ("D:/upfile.exe ")
The complete code is as follows:
1 import OS 2 from selenium import webdriver 3 import time 4 class Connect (): 5 def _ init _ (self, UserName, PassWord, URL): 6 self. userName = UserName 7 self. passWord = PassWord 8 self. URL = URL 9 def connect (self): 10 self. driver = webdriver. firefox () 11 self. driver. get (self. URL) 12 self. driver. find_element_by_xpath ('/html/body/div [1]/div [3]/div [6]/div [6]/div [2]/'). click () 13 self. driver. find_element_by_xpath ('/html/body/div [1]/div [3]/div [6]/div [3]/form/p [5]/input '). send_keys (self. userName) 14 self. driver. find_element_by_xpath ('/html/body/div [1]/div [3]/div [6]/div [3]/form/p [6]/input '). send_keys (self. passWord) 15 self. driver. find_element_by_xpath ('/html/body/div [1]/div [3]/div [6]/div [3]/form/p [9]/input '). click () 16 # Set the thinking time 17 time. sleep (30) 18 sreach_window = self. driver. current_window_handle # This line of code is used to locate the current page 19 self. driver. find_element_by_xpath ('// * [@ id = "h5Input0"]'). click () 20 OS. system (r "C: \ Users \ zg \ Desktop \ upfile.exe") 21 Connect (UserName, PassWord, URL ). upload ()
View Code
This article references: http://blog.csdn.net/justheretobe/article/details/50939021