catalogue
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 driver.switch_to.frame("671a7d8e-d2a0-43fd-8b23-0901a7b8f687")
After locating the element for operation, you need to return to the Top window
// An highlighted block driver.switch_to.default_content()
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') time.sleep(1) ActionChains(driver).move_to_element(mouse).perform() try: driver.find_element_by_xpath("//*[@id='wrapper']/div[2]/div/div[2]/div[2]/div/div/div/div[2]").click() print("Export succeeded") except: time.sleep(1)
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 driver.switch_to.window(currentWin[-1]) 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: https://www.autoitscript.com/site/autoit/
Use the reference link article: https://www.cnblogs.com/generalli2019/p/11451211.html
The editor code is as follows
WinActivate("open"); ;ControlFocus("title","text",controlID) Edit1=Edit instance 1 ControlFocus("open", "","Edit1") ; Wait 10 seconds for the Upload window to appear WinWait("open","[CLASS:#32770]",10) ; Set the File name text on the Edit field ControlSetText("open", "", "Edit1", "D:\Users\00762856\Downloads\Failed to pass the review.pdf") Sleep(2000) ; Click on the Open button ControlClick("open", "","Button1");
Add the following statement to the automation code
import os os.system(r'D:\work\uitest2\test_zhifu2\autoit\test.exe')
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 time.sleep(3)
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 time.sleep(3)
Overall code
// An highlighted block driver = webdriver.Chrome() driver.get('http://10.39.168.111/webroot/decision') time.sleep(2) driver.maximize_window() time.sleep(1) driver.find_element_by_xpath("//*[@id='wrapper']/div[1]/div/div[2]/div/div/div[1]/div[1]/div[1]/div[1]/div[2]/input").clear() driver.find_element_by_xpath("//*[@id='wrapper']/div[1]/div/div[2]/div/div/div[1]/div[1]/div[1]/div[1]/div[2]/input").send_keys("00744521") driver.find_element_by_xpath("//*[@id='wrapper']/div[1]/div/div[2]/div/div/div[1]/div[2]/div[1]/div[1]/div[2]/input").clear() driver.find_element_by_xpath("//*[@id='wrapper']/div[1]/div/div[2]/div/div/div[1]/div[2]/div[1]/div[1]/div[2]/input").send_keys("fwD.342p9IerwK") driver.find_element_by_xpath("//*[@id='wrapper']/div[1]/div/div[2]/div/div/div[1]/div[4]/div").click() time.sleep(3) driver.find_element_by_xpath("//*[@id='wrapper']/div[1]/div[1]/div[2]/div/div[2]/div[2]/div[1]/div[5]/div/div/div[1]/div/div/div/div/div/div/div[4]").click() driver.find_element_by_xpath("//*[@id='wrapper']/div[1