Selenium Upload file Method supplement--sendkeys, Win32gui

Source: Internet
Author: User

Before and everyone said the input tag of the way to upload files:

"Selenium2 Python Automation Test" (13)--Upload file

Now many websites upload the label is not input, but Div and the like:

All are div tags, this time with the above method is not good use. Today the captain has learned two ways with the help of the great God, and now the captain teaches you how to upload a div tag.

The most convenient--sendkeys

Everyone just need pip install SendKeys Library, and then the code directly with the OK, here with the big fish upload video as an example of the code:

Note that the browser uses Firefox, because Chrome will not support OH ~

# Coding:utf-8

From seleniumImport Webdriver
From timeImport Sleep
Import SendKeys

# options = Webdriver. Chromeoptions ()
# options.add_argument (' Disable-infobars ')
# d = webdriver. Chrome (chrome_options=options)
D = Webdriver. Firefox () D.get ("Https://mp.dayu.com")
# cookie Login, skip verification code
D.add_cookie ({' Name ':' CNA ',
' Value ':' Fhsvglvkf3gz1 ',
' Domain ':'. dayu.com '}) D.add_cookie ({ ' name ': ' ISG ',
              ' value ': ' biagbw7swwbt6-_ W8k5 ',
              ' domain ': '. dayu.com '} ' D.add_cookie ({ ' Domain ': ' mp.dayu.com ',
              ' name ': ' user_tmp ',
& nbsp             ' value ': ' TUAAJFSA8AAXABK '}) sleep ( 3) D.refresh () Sleep ( 2) D.find_element_by_xpath ( "/html/body/div/div/div[1]/ul/li[2]/ul/li[2]/a/span"). Click () Sleep ( 5) D.find_element_by_xpath ( '/html/body/div[1]/div[3]/div/div[2]/div/div/div/div/div[2]/div[1]/div[1 ]. Click () Sendkeys.sendkeys ( R ' C:\Users\DELL\Desktop\vid.mp4 ') sleep ( 2) Sendkeys.sendkeys ( "{ ENTER} ")
             

The second method: Win32gui

This method is a bit cumbersome, and downloading the library is very troublesome ...

To install

1, Pywin32-218.win-amd64-py2.7.exe

2, Pypiwin32

Once you've downloaded the two libraries, you'll be ready to code:

# Coding:utf-8

Import Win32gui
Import Win32API
Import Win32con
From seleniumImport Webdriver
From timeImport Sleep

# options = Webdriver. Chromeoptions ()
# options.add_argument (' Disable-infobars ')
# d = webdriver. Chrome (chrome_options=options)

D = Webdriver. Firefox () D.get ("Https://mp.dayu.com") D.add_cookie ({' Name ':' CNA ',
' Value ':' Fhw6elakvkf3gz1 ',
' Domain ':'. dayu.com '}) D.add_cookie ({' Name ':' ISG ',
' Value ':' Biagbw7sipskiv6-_w8k5 ',
' Domain ':'. dayu.com '}) D.add_cookie ({' Domain ':' Mp.dayu.com ',
' Name ':' User_tmp ',
' Value ':' TUAAJFSAWOKIQUTU9MIKYBK '}) sleep (3) D.refresh () Sleep (2) D.find_element_by_xpath ( "/html/body/div/div/div[1]/ul/li[2]/ul/li[2]/a/span"). Click () Sleep ( 5) D.find_element_by_xpath ( '/html/body/div[1]/div[3]/div/div[2]/div/div/div/div/div[2]/div[1]/div[1] '). Click () dialog = Win32gui. FindWindow ( ' #32770 ', u ' file Upload ') ComboBoxEx = Win32gui. FindWindowEx (Dialog, 0, ' ComboBoxEx32 ', None) ComboBox = Win32gui. FindWindowEx (ComboBoxEx, 0, ' ComboBox ', None) Edit = Win32gui. FindWindowEx (ComboBox, 0, ' Edit ', None) button = Win32gui. FindWindowEx (Dialog, 0, ' Button ', None) sleep ( 2) Win32gui. SendMessage (Edit, Win32con. Wm_settext, 0, R ' C:\Users\DELL\Desktop\vid.mp4 ') sleep ( 2) Win32gui. SendMessage (Dialog, Win32con. WM_COMMAND, 1, button)
             

The captain of the test is available, everyone also try ~ ~

Public search "automated test Practice" or scan the QR code below to add attention ~ ~ ~

Selenium Upload file Method supplement--sendkeys, Win32gui

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.