Selenium+Python download and upload files

Posted by Sorrow on Mon, 14 Feb 2022 07:05:16 +0100


This article records the automated test script I wrote using Selenium. The function is to download files from the web page and upload the downloaded files to another page. At the same time, some problems and solutions encountered in the process of writing are recorded

Download file function

Google browser download settings

Enter the setting page - > advanced, and check [ask for the storage location of each file before downloading] of the downloaded content to ensure that the name and storage address of the downloaded file can be changed later.

Switch iframe framework

When locating the Export button, webdriver reports an error when processing the location element, indicating that there is no such element. After query, it is found that the page uses frame technology. The web page is divided into several sub pages. The Top window layer is the default element searched by webdriver. The exported element is embedded in the frame. You need to use the switch to statement to switch to the frame, and then operate the element. After the operation, you need to switch out to the default area, that is, the Top window.

The contents behind the red box position label in the above figure are all in the iframe framework. Locate the label through the name attribute of the label. The code is as follows

// An highlighted block

After locating the element for operation, you need to return to the Top window

// An highlighted block

Mouse hover element positioning

Use hover to place the mouse on the exported element and pop up options

Mouse over move_to_element()

// An highlighted block
mouse = driver.find_element_by_xpath('//*[@class="bi-show bi-abs"]/div[2]/div/div[2]/div')
# a = driver.find_elements_by_xpath('//body/*[@id="wrapper"]/div[2]/div/div[2]/div')
    print("Export succeeded")

Element positioning after mouse hovering

If you need to locate the element that is displayed only when you hover over the mouse, if you directly click to obtain the element address, the option will disappear after you move the mouse away, so you can't locate it. First hover the mouse and the option to locate appears. Then, in Google browser F12, select sourse and click the red box button in the figure below to enter the Debugger paused mode. At this time, locate the element again, and the option will not disappear.

Upload file

Switch handle

When a new page pops up, you need to get new page elements. You need to switch the page to a new web page window first
1. Get all handles

// An highlighted block
currentWin = driver.window_handles
print(f"Get all current handle:{currentWin}")

2. Switch to a new page

// An highlighted block
handle = driver.current_window_handle
print(f"Get current handle:{handle}")//Confirm that you have switched to a new page

Using AutoIt to locate desktop elements

1. Download, install and use AutoIT
Official website:
Use the reference link article:
The editor code is as follows


;ControlFocus("title","text",controlID) Edit1=Edit instance 1
ControlFocus("open", "","Edit1")

; Wait 10 seconds for the Upload window to appear

; Set the File name text on the Edit field
  ControlSetText("open", "", "Edit1", "D:\Users\00762856\Downloads\Failed to pass the review.pdf")


; Click on the Open button
  ControlClick("open", "","Button1");

Add the following statement to the automation code

import os

2. Parameter transfer parameterization (not implemented)
Modify the file address "D:\Users"D: \ users \ 00762856 \ downloads 762856\Downloads \ failed approval. pdf" in the editor code to a parameter $CmdLine[1]
Add in code

file_path = "D:\Users\00762856\Downloads\Failed to pass the review.pdf"
os.system(r'D:\work\uitest2\test_zhifu2\autoit\test.exe' % file_path)  # Your own local address

3. Batch upload (not realized)
Method 1: first put the image to be uploaded under a list, and then for loop

# Put the pictures to be uploaded under a list
all_png = ["D:\\1.png", "D:\\2.png", "D:\\3.png", "D:\\4.png"]
# Click repeatedly to upload pictures
for i in all_png:
    # 1 Click to open the editor picture 
    # 2. Click the file upload button
    # Execute autoit to upload files
    os.system("C:\Users\Gloria\Desktop\cmdjpg.exe %s" % i)  # Your own local exe path

Method 2: number the pictures to be uploaded, such as: 0 png , 1. png, 2. Png (numbering from 0), put it in the same directory, and then loop for

# Click repeatedly to upload pictures
for i in range(4):
    # 1 Click to open the editor picture

    # 2. Click the file upload button

    # file name
    file_name = "D:\\%s.png" % i  # Parameterized path name

    # Execute autoit to upload files
    os.system("C:\Users\Gloria\Desktop\cmdjpg.exe %s" % file_name)  # Your own local exe path

Overall code

// An highlighted block
driver = webdriver.Chrome()

Topics: Python Javascript Selenium