Python webdriver File Upload

Source: Internet
Author: User

Yesterday wrote the Web file download UI Automation, after downloading, today will write the Web file upload function.

Of course it took two hours to upload the success. Here are the steps to write your own action

First of all, there are many ways to talk online.

If the input tag is best done, navigate directly to the element and then SendKeys ("value") to

<id= "file_name"  class= "Text-1 w255"  type= "text"  readonly= ""  value= "" data-file= ""/> 

But the research and development of the students made the ReadOnly attribute of input, good learning a little, this attribute of input can not use SendKeys.

There's only another way,

Use the following webdriver for Python to simulate the keyboard operation

            #The following method did not succeed            #actionchains (self.driver). Send_keys (Releasefile). Perform ()            #Ctrl + a            #actionchains (self.driver). Key_down (Keys.control). Send_keys (' a ') . KEY_UP (Keys.control). Perform ()            #self.driver.implicitly_wait (5)            #actionchains (self.driver). Key_down (Keys.control). Send_keys (' C '). KEY_UP (Keys.control). Perform ()            #self.driver.implicitly_wait (5)            #actionchains (self.driver). Key_down (Keys.control). Send_keys (' V '). Key_up (Keys.control). Perform ()            #Time.sleep (2)            #actionchains (self.driver). Key_down (Keys.enter). Perform ()            #Self.driver.find_element_by_xpath (".//*[@id = ' file_name ']"). Send_keys (Releasefile)

found that the mouse is actually working on the page, rather than the pop-up file selection box, so this way still does not work.

Last reference

Python Selenium File Upload Method summary

Http://www.jb51.net/article/92678.htm

SendKeys is used because other methods are required to install other programs

The first thing to install is the SendKeys library, which can be installed with PIP

Pip Install SendKeys

The installation process encountered the following problems

Need to download to http://aka.ms/vcpython27 at this time

https://www.microsoft.com/en-us/download/details.aspx?id=44266

The installation SendKeys can then be performed successfully.

The code is as follows

#-*-coding:utf-8-*-#time:2017/7/1-15:47#Author:yangyangjun#-*-coding:utf-8-*-ImportSendKeys#Click Upload to download attachmentsTime.sleep (2) Self.driver.find_element_by_xpath (".//*[@id = ' down_bar_code_template ')"). Click () self.driver.implicitly_wait (5) #releaseFile_Path= Os.path.join (OS.GETCWD (),'Test_data') Releasefile= R'C:\PySpace\CMS\UiTest\test_data'+'\ui_release01.xlsx'Time.sleep (2) self.driver.implicitly_wait (5) Self.driver.find_element_by_css_selector (". Webuploader-pick"). Click () time.sleep (2)            #Enter the pathSendkeys.sendkeys (releasefile) time.sleep (2)            #Determine path inputSendkeys.sendkeys ("{ENTER}") Time.sleep (2)            #OK Open buttonSendkeys.sendkeys ("{ENTER}") Time.sleep (2) self.driver.implicitly_wait (5)            #Click UploadSelf.driver.find_element_by_xpath (".//*[@id = ' file_upload_btn ')"). Click () time.sleep (6)            #prompt message, upload successfulSuccesstest = u"Upload Product success! "Get_reinfo= Self.driver.find_element_by_xpath (".//*[@id = ' step_05 ']/ul/li[1]"). TextifSuccesstest = =Get_reinfo:PrintU"Upload Success! "                #Click to view ProductsSelf.driver.find_element_by_xpath (".//*[@id = ' view_goods ')"). Click () druglist= ['azithromycin tablets']                #invoke validation and delete functionsself.get_verify (druglist)Else:                PrintU"upload failed! "        Else:            PrintU"failed to enter seller center"

Effects such as

The first simulation return is to confirm the input file path, and the second carriage return is to confirm clicking on the Open button.

The upload function for this file is now complete.

Online says it's a little shaky, and there's time to learn about other ways.

Python webdriver File Upload

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.