Python uploads files to Baidu cloud (for example ),

Source: Internet
Author: User

Python uploads files to Baidu cloud (for example ),

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:

# Select the account and password to log on to the driver. find_element_by_xpath ('/html/body/div [1]/div [3]/div [6]/div [6]/div [2]/'). click () # log on to the driver. find_element_by_xpath ('/html/body/div [1]/div [3]/div [6]/div [3]/form/p [5]/input '). send_keys ('username') driver. find_element_by_xpath ('/html/body/div [1]/div [3]/div [6]/div [3]/form/p [6]/input '). send_keys ('Password') 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:

# Upload 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.

Obtain the control information of the text box:

Obtain the control information of the Open button:

5. Compile an AutoIt script to upload files.

1. Open scite script editor

2. Code:

; ControlFocus ("title", "text", controlID) Edit1 = Edit instance 1 ControlFocus ("File Upload", "", "Edit1 "); wait 10 seconds for the Upload window to appearWinWait ("[CLASS: #32770]", "", 10); Set the File name thext on the Edit fieldControlSetText ("File Upload ", "", "Edit1", "D: \ test.txt") Sleep (2000); Click on the Open buttonControlClick ("File Upload", "", "Button1 ");

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.

# Click Upload to open the file upload window driver. find_element_by_xpath ('/html/body/div [1]/div [2]/div [1]/div [2]/div [2]/div [2]/[ 1]/form/input '). click () # Use the autoit script to automatically upload files # import the python OS library file: import osos. system ("D:/upfile.exe ")

The complete code is as follows:

Import osfrom selenium import webdriverimport timeclass Connect (): def _ init _ (self, UserName, PassWord, URL): self. userName = UserName self. passWord = PassWord self. URL = URL def connect (self): self. driver = webdriver. firefox () self. driver. get (self. URL) self. driver. find_element_by_xpath ('/html/body/div [1]/div [3]/div [6]/div [6]/div [2]/'). click () self. driver. find_element_by_xpath ('/html/body/div [1]/div [3]/div [6]/div [3]/form/p [5]/input '). send_keys (self. userName) self. driver. find_element_by_xpath ('/html/body/div [1]/div [3]/div [6]/div [3]/form/p [6]/input '). send_keys (self. passWord) self. driver. find_element_by_xpath ('/html/body/div [1]/div [3]/div [6]/div [3]/form/p [9]/input '). click () # Set the thinking time. sleep (30) sreach_window = self. driver. current_window_handle # This line of code is used to locate the current page self. driver. find_element_by_xpath ('// * [@ id = "h5Input0"]'). click () OS. system (r "C: \ Users \ zg \ Desktop \ upfile.exe") Connect (UserName, PassWord, URL ). upload ()

The above Python Implementation of Baidu cloud's file upload (example) is all the content shared by Alibaba Cloud xiaobian. I hope to give you a reference, and I hope you can provide more support to the customer's house.

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.