selenium -- upload files based on python

Through online data query, temporarily record two methods of file upload:
test URL: http://www.sahitest.com/demo/php/fileUpload.htm
1. Through the send_keys method (this method is only applicable to the input tag)
1. View the element tag of the upload button as input tag
write picture description here
2. Use the following code directly:

# _*_ coding=utf-8 _*_
from selenium import webdriver
import os

url = "http://www.sahitest.com/demo/php/fileUpload.htm"
driver = webdriver.Chrome()
driver.get(url)
driver.find_element_by_xpath('//*[@id="file4"]').send_keys("C:\\Users\\saas\\Desktop\\ump.xlsx")

2. Handling the problem of uploading files through autoit (applicable to all)
1. For the installation and use of autoit, please refer to: https://www.cnblogs.com/fnng/p/4188162.html
2. Due to different browsers, the obtained The title of the pop-up window is different, so it is necessary to distinguish it by passing the parameters.

;first make sure the number of arguments passed into the scripts is more than 1
If $CmdLine[0]<2 Then Exit EndIf ;if parmas num <2 ,then break
;$CmdLine[0] ;参数的数量
;$CmdLine[1] ;第一个参数 (脚本名称后面)
;$CmdLine[2] ;第二个参数
;都是从cmd传入参数
 handleUpload($CmdLine[1],$CmdLine[2])

;定义上传函数,有两个参数,第一个是浏览器名字,第二参数是文件路径
 Func handleUpload($browser, $uploadfile)
     Dim $title                          ;定义一个title变量
            ;根据浏览器来判断弹窗的title来判断是什么
            If $browser="ie" Then                          ; 代表IE浏览器
                  $title="选择要加载的文件"
            ElseIf $browser="chrome" Then               ; 代表谷歌浏览器
                  $title="打开"
            ElseIf $browser="firefox" Then             ; 代表火狐浏览器
                  $title="文件上传"
            EndIf

            if WinWait($title,"",4) Then ;等待弹出出现,最大等待时间是4
                   WinActivate($title)                  ;找到弹出窗口之后,激活当前窗口
                   ControlSetText($title,"","Edit1",$uploadfile)   ;把文件路径放入输入框,此”Edit1“是用FinderTool获取到的
                   Sleep(1000)
                   ControlClick($title,"","Button1")                ;点击保存或者打开或者上传按钮,此“Button1”使用FinderTool获取到的
            Else
            Return False
            EndIf
 EndFunc

3. Compile the code of #2 above into an exe file for code calling
4. Call the exe file compiled by #3 through the python code to process the upload pop-up window, and pass whatever parameters are passed in what browser is used

from selenium import webdriver
import os
import time

url = "http://www.sahitest.com/demo/php/fileUpload.htm"
driver = webdriver.Chrome()
driver.get(url)
driver.find_element_by_xpath('//*[@id="file"]').click()
command = "C:\\upload.exe" + " " + "chrome" + " " + "C:\\ump.xlsx"
os.system(command)
time.sleep(3)
driver.close()

Since the time to wait for the pop-up window to appear has been added in #2, the waiting time is not added in python. If necessary, you need to add time to wait to reduce the error rate of the script. Autoit
's exe file download address: https://download.csdn.net/download/lb245557472/10370909
3. Other methods
You can refer to:
https://blog.csdn.net/huilan_same/article/details/52439546

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324822509&siteId=291194637