Python-based selenium two file upload

Method, input tag upload

    If input label, you can directly enter the path, you can directly call send_keys input path, do not make too much to say, the foregoing related method of operation.

Second method, a non-input tag uploads

    This approach requires the aid of third-party tools to upload, mainly in the following three conditions:

    1.AutoIt to call au3 or exe format files it generates

    2.SendKeys third-party libraries (currently only supports up to version 2.7)

      URL: https: //pypi.python.org/pypi/SendKeys

    3.Python pywin32 the library dialog box operates by identifying handles

    Installation pywin32 library because larger files, it is recommended to use watercress source directly pip installation:

pip install -i https://pypi.douban.com/simple pywin32

    In the first two are not considered here, you can only use the third method, you can use tools winspy before using assisted positioning, winspy Download: https://sourceforge.net/projects/winspyex/

    winspy positioning method uses a tool path absolute positioning, i.e., the root path to the pop-up button click Upload entire window page , generally positioned in accordance with the value and Class The Text property , as shown:

    As shown in FIG root path, its Text Text is "on", Class attribute value "# 32770."

    In the top left corner there is a focus WinSpy tool button, drag to a corresponding position (e.g., file path input box) popup uploaded by dragging release, WinSpy interface displays Text Text value of the current position (here empty ) and Class attribute value (the Edit), the windows in the window, the value of the Parent property, represented in parentheses on the Class property value element layer, a layer of the element can jump by clicking Parent property value, the final we locate the file path input box Class attribute path: Edit - combox - comboBoxEx32 - # 32770 .

 

 

 

     So the same token, the "Open" button to locate the path by the same absolute way to get it to Class attribute path: the Button - # 32770 .

 

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

    pywin32 implementation steps are:

    1, to find the absolute path elements box and enter the "Open" button;

    2, enter an absolute path, click Open

    Prerequisites for the realization of the page: windows upload window has appeared, may sleep1 ~ 2 second wait uploaded bomb box appears.

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/xiaogongjin/p/11546518.html