One, "Upload files"
To locate the upload button, pass send_keys add local file path on it. Both absolute and relative paths are possible, and the key is that the uploaded file exists.
#coding =utf-8
From selenium import Webdriver
Import Os,time
Driver = Webdriver. Firefox ()
#打开上传文件页面
File_path = ' file:///' + os.path.abspath (' upload_file.html ')
Driver.get (File_path)
#定位上传按钮, add a local file
Driver.find_element_by_name ("file"). Send_keys (' D:\\selenium_use_case\upload_file.txt ')
Time.sleep (2)
Driver.quit ()
Note: the Send_keys () method can also be followed by a local file path in addition to the input content. To achieve the purpose of uploading files
Second, "Download the file"
Import requests
Print Requests.head (' http://www.python.org '). headers[' Content-type ']
#coding =utf-8
Import OS
From selenium import Webdriver
fp = Webdriver. Firefoxprofile ()
Fp.set_preference ("Browser.download.folderList", 2)
Fp.set_preference ("browser.download.manager.showWhenStarting", False)
Fp.set_preference ("Browser.download.dir", OS.GETCWD ())
Fp.set_preference ("Browser.helperApps.neverAsk.saveToDisk",
"Application/octet-stream")
Browser = Webdriver. Firefox (FIREFOX_PROFILE=FP)
Browser.get ("Http://pypi.python.org/pypi/selenium")
Browser.find_element_by_partial_link_text ("Selenium-2"). Click ()
The browser.download.dir is used to specify the directory where you downloaded the file.
OS.GETCWD () The function does not need to pass parameters to return the current directory.
Application/octet-stream is the type of content
"Selenium Automation-upload and download"