Selenium-webdriver (Python) (ix) upload file

Source: Internet
Author: User
Tags file upload min sleep

This section focuses on:

Uploading files

File upload operation is also one of the most common functions, upload function is not used to new methods or functions, the key is thinking.

The upload process typically opens a local window and selects the local file to add from the window. So, generally will card in how to operate the local window to add upload files.

In fact, in selenium webdriver not so complicated as we think; just locate the upload button and Send_keys add the local file path to it. Absolute path and relative path are all OK, the key is to upload the file exists. The following is an example demo.

Upload_file.html

 

upload.py

#coding =utf-8 from
Selenium import webdriver
import os,time

driver = webdriver. Firefox ()

#脚本要与upload_file. HTML same directory
File_path =  ' file:///' + os.path.abspath (' upload_file.html ')
Driver.get (File_path)

#定位上传按钮, add local file
driver.find_element_by_name ("file"). Send_keys (' D:\\selenium_use_ Case\upload_file.txt ')
time.sleep (2)

driver.quit ()

Other applications are not easy to find, so just create your own pages, which is troublesome, but the scripting code highlights the focus.

Here to find 1139 instances of mailboxes, have an account of the students can test the ~!

(landing basic version of the 139 mailbox, network disk module upload file.) )

139upload.py

#coding =utf-8 from
Selenium import webdriver
import os,time

driver = webdriver. Firefox ()

driver.get ("http://m.mail.10086.cn")
driver.implicitly_wait

#登陆
driver.find_ element_by_id ("ur"). Send_keys ("mobile number")
driver.find_element_by_id ("PW"). Send_keys ("password")
Driver.find_ Element_by_class_name ("Loading_btn"). Click ()
time.sleep (3)

#进入139网盘模块
driver.find_element_by_ XPath ("/html/body/div[3]/a[9]/span[2]"). Click ()
time.sleep (3)

#上传文件
driver.find_element_by_id (" Id_file "). Send_keys (' d:\\selenium_use_case\upload_file.txt ')
time.sleep (5)

driver.quit ()

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

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.