selenium upload operation of the operating elements (F)

Upload operations are divided into two cases:

1, input tag upload

If input path can be entered directly, then directly call send_keys input path, and elements similar to the operations front, not too many repeat them here.

2, non-input tag uploads

Non-input tag uploads, you need to use third-party tools:

Here we use third-party libraries pywin32 python library to identify the dialog box handle, and then upload operation

First install third-party libraries: pip install pywin32

With winspy to assist positioning, winspy Download: https://sourceforge.net/projects/winspyex/

Absolutely positioned winspy tool use, and selenium elements positioned in different ways

 

By locating the path of the corresponding element of the aid winspy tool, can be accomplished by pywin32 library upload it!

1, filePath is the absolute path where the uploaded file

2, if your browser is Google so do not bother, if other browsers, see what you upload pop-up window of the upper left corner of the name is written to else where title = "" inside.

3, when Windows encounters an element positioning window to upload, you can call this method directly: upload (C: \ file upload .txt)

For example, the Firefox browser and Google is different from its title = "file upload"

 

 

Import win32gui
 Import win32con 


DEF Upload (filePath, browser_type = " Chrome " ):
     '' ' 
    operation implemented by the file upload module pywin32 
    : param filePath: the absolute path of the file 
    : param browser_type: browser type (default Chrome) 
    : return : 
    '' ' 
    IF browser_type == " Chrome " : 
        title = " open " 
    the else : 
        title = ""   # here be modified depending on the type of browser 

    # find elements 
    # a window "32770", "open"
    dialog = win32gui.FindWindow(" # 32770 " , title)
     # propagated downward 
    ComboBoxEx32 = win32gui.FindWindowEx (Dialog, 0, " ComboBoxEx32 " , None)   # two 
    the comboBox = win32gui.FindWindowEx (ComboBoxEx32, 0, " the ComboBox " , None)    # three 
    # edit button 
    edit = win32gui.FindWindowEx (the comboBox, 0, ' the edit ' , None)   # four 
    # open button 
    button = win32gui.FindWindowEx (Dialog, 0, ' the button ' , " open (O &) ")  # Two 

    # absolute path to the file, click "Open" button 
    win32gui.SendMessage (Edit, win32con.WM_SETTEXT, None, filePath)   # send the file path 
    win32gui.SendMessage (Dialog, win32con.WM_COMMAND, 1, the Button)   # Click to open push button

 

Guess you like

Origin www.cnblogs.com/xingyunqiu/p/11558936.html