Selenium implement File Upload Method summary (AutoIt, Win32gui, Sengkeys)---based on python

Source: Internet
Author: User

When you use selenium for UI Automation testing, you often encounter a problem with local file uploads, and there are generally two scenarios for solving this problem:

1. Element tag is input

2. Non-input upload

Let's take a case study of two different cases respectively.

(i) element tag is input

This kind of case processing is relatively simple, the tag is the upload of input, can be directly through the Send_keys ("Local File path") implementation

Example: (Take Baidu network disk for example)

1 #Coding:utf-82  fromSeleniumImportWebdriver3  fromTimeImportSleep4 #declares the configuration file path, preceded by the path with the R ID, followed by the path original output, if not added R need to escape the path of the slash5Profile_directory=r"C:\Users\55348\AppData\Roaming\Mozilla\Firefox\Profiles\tqc0968l.default"6 #Load configuration file7profile=Webdriver. Firefoxprofile (profile_directory)8 #Start Browser Configuration9Driver=Webdriver. Firefox (Profile)Ten #Open URL OneDriver.get ("http://pan.baidu.com") A #Implicit wait -Driver.implicitly_wait (10) - #Click Upload, because the upload button is the input property, so you can directly through the Send_keys theDRIVER.FIND_ELEMENT_BY_ID ("h5input0"). Send_keys (R"C:\Users\55348\Desktop\03913f358d9be352bd125ae7087dd0d6.apk") -Sleep (10) - #determine if the upload was successful -New=driver.find_elements_by_xpath ("//*[@title = ' 03913f358d9be352bd125ae7087dd0d6.apk ' and @class = ' Xj9qoe ')") + ifLen (new) ==1: -     Print "upload apk OK" + Else: A     Print "Upload apk failed" atDriver.quit ()
< Span style= "COLOR: #8f9d6a" > < Span style= "COLOR: #cda869" > < Span style= "COLOR: #f9ee98" >&NBSP;            

(ii) non-input situation

This situation is more complex, with three treatments: ① using SendKeys third-party libraries ② using autoit third-party tools ③ using Win32 GUI Tools

1.1 Using SendKeys third-party libraries

First SendKeys third-party libraries

Pip Install SendKeys

If you get a hint like this: Try a few more times.

If the following prompt appears:, this situation can be prompted by the get it from http://aka.ms/vcpython27

That is, log in to this address download C + +, after the download is complete, fool installation.

Second, after the third-party library installation is complete, you can upload the action, as follows:

1 #Coding:utf-82  fromSeleniumImportWebdriver3  fromTimeImportSleep4 ImportSendKeys5  fromSelenium.webdriver.common.keysImportKeys6 #declares the configuration file path, preceded by the path with the R ID, followed by the path original output, if not added R need to escape the path of the slash7Profile_directory=r"C:\Users\55348\AppData\Roaming\Mozilla\Firefox\Profiles\tqc0968l.default"8 #Load configuration file9profile=Webdriver. Firefoxprofile (profile_directory)Ten #Start Browser Configuration OneDriver=Webdriver. Firefox (Profile) A #Open URL -Driver.get ("http://pan.baidu.com") - #Implicit wait theDriver.implicitly_wait (10) - #still take Baidu network disk as an example, this time by clicking the upload button, upload file selection -DRIVER.FIND_ELEMENT_BY_ID ("h5input0"). Click () -Sendkeys.sendkeys (R"C:\Users\55348\Desktop\03913f358d9be352bd125ae7087dd0d6.apk") +Sleep (2) -Sendkeys.sendkeys ("{ENTER}") #enter键 +Sleep (2) ASendkeys.sendkeys ("{ENTER}") atSleep (5) - #determine if the upload was successful -New=driver.find_elements_by_xpath ("//*[@title = ' 03913f358d9be352bd125ae7087dd0d6.apk ' and @class = ' Xj9qoe ')") - ifLen (new) ==1: -     Print "upload apk OK" - Else: in     Print "Upload apk failed" -Driver.quit ()

1.2 Using the AutoIt tool

The following article is excerpted from http://blog.csdn.net/huilan_same/article/details/52208363

1.3 Using the Win32 GUI

The following article is excerpted from http://blog.csdn.net/huilan_same/article/details/52439546

Note:

  Multiple file uploads, you can add a file path to the list and then read and upload it through a for loop

Upload_directory=[]

Upload_directory.append[r "C:\1.txt"]

Upload_directory.append[r "C:\2.txt"]

Upload_directory.append[r "C:\3.txt"]

For file in Upload_directory:

......

 < Span style= "COLOR: #cda869" > < Span style= "COLOR: #f9ee98" >&NBSP;        

Selenium implement File Upload Method summary (AutoIt, Win32gui, Sengkeys)---based on python

Related Article

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.