程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Selenium+python downloading and uploading files

編輯:Python

Selenium+Python Download files and upload files

  • Catalog
    • Download file function
      • Google browser download settings
      • Switch iframe frame
      • Mouse hover element positioning
      • Element positioning after mouse hovering
    • Upload files
      • Switch handle
      • utilize AutoIt Locate desktop elements
    • The overall code

Catalog

This article records what I have done with Selenium Write automated test scripts , The function is to download files from web pages , And upload the downloaded file 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

Go to the settings page -> senior , Check the 【 Ask where to save each file before downloading 】 Check for , To ensure that the name of the downloaded file and the save address can be changed later .

Switch iframe frame

Appears when you locate the Export button webdriver An error is reported when processing the positioning element , Prompt the problem 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 .webdriver The default search elements are Top window This floor . The export element is embedded in frame Inside , You need to use it first switch to sentence , Switch to frame Inside , Then operate on the elements , After the operation , need switch out To default Area , That is to say Top window.

The contents behind the red box position label in the above figure are all in iframe In the frame , By tagging name attribute , Locate this label , The code is as follows

// An highlighted block
driver.switch_to.frame("671a7d8e-d2a0-43fd-8b23-0901a7b8f687")

After positioning the element for operation , Need to go back Top window

// An highlighted block
driver.switch_to.default_content()

Mouse hover element positioning

Use hover to place the mouse over the exported element , Pop up options

Mouse hovering 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

Elements that need to be positioned for mouse over display , If you click directly to get the element address , When the mouse is removed, the option disappears and cannot be located . First, hover the mouse and the option to locate appears , Then in Google Chrome F12, choice Sourses Click the red box button in the figure below , Get into Debugger paused Pattern . At this point, locate the element , Options will not disappear .

Upload files

Switch handle

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

// An highlighted block
currentWin = driver.window_handles
print(f" Get all the 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 the new page 

utilize AutoIt Locate desktop elements

1.AutoIt Download, install and use
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\ The audit failed .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 ( Unrealized )
The file address in the editor code “D:\Users\00762856\Downloads\ The audit failed .pdf”, Change to a parameter $CmdLine[1]
Add... To the code

file_path = "D:\Users\00762856\Downloads\ The audit failed .pdf"
os.system(r'D:\work\uitest2\test_zhifu2\autoit\test.exe' % file_path) # Your own local address

3. Batch upload ( Unrealized )
Method 1 : First put the picture to be uploaded into a list Next , then for loop

# Put the pictures you need to upload into a list Next
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 on the editor image
# 2 Click the file upload button
# perform autoit Upload files
os.system("C:\Users\Gloria\Desktop\cmdjpg.exe %s" % i) # Your own local .exe route
time.sleep(3)

Method 2 : Number the pictures to be uploaded , Such as :0.png , 1.png, 2.png such ( from 0 Numbered starting ), Put it in the same directory , then for loop

# Click repeatedly to upload pictures
for i in range(4):
# 1 Click on the editor image
# 2 Click the file upload button
# file name
file_name = "D:\\%s.png" % i # Parameterized path name
# perform autoit Upload files
os.system("C:\Users\Gloria\Desktop\cmdjpg.exe %s" % file_name) # Your own local .exe route
time.sleep(3)

The 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

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved