Automatically uploads attachments without manual intervention. Manually uploads attachments

Source: Internet
Author: User

Automatically uploads attachments without manual intervention. Manually uploads attachments

<Html> 

1. Use send_keys of webdriver to upload files

#! Usr/bin/env python #-*-coding: UTF-8-*-"@ author: sleeping_cat @ Contact: zwy24zwy@163.com "# automatically upload attachments without manual intervention # Use the send_keys method of webdriver to upload files from selenium import webdriverimport unittestimport timeimport tracebackfrom selenium. webdriver. support. ui import WebDriverWaitfrom selenium. webdriver. common. by import Byfrom selenium. webdriver. support import expected_conditions as ECfrom selenium. common. exceptions import TimeoutException, NoSuchElementExceptionclass TestDemo (unittest. testCase): def setUp (self): self. driver = webdriver. chrome () def test_uploadFileBySendKeys (self): url = "e: \ uploadFile.html" self. driver. get (url) try: wait = WebDriverWait (self. driver, 10, 0.2) # create a wait display wait object. until (EC. element_to_be_clickable (. ID, 'file') # Wait until you determine whether the Upload file button on the tested page is clickable. Wait t TimeoutException as e: print (traceback. print_exc () failed t NoSuchElementException as e: print (traceback. print_exc () failed t Exception as e: print (traceback. print_exc () else: fileBox = self. driver. find_element_by_id ('file') fileBox. send_keys ('C: \ test.txt ') # enter the file path c: // test.txt time in the file upload box. sleep (3) fileSubmitButton = self. driver. find_element_by_id ('filesubmit ') fileSubmitButton. click () def tearDown (self): self. driver. quit () if _ name _ = '_ main _': unittest. main ()

2. Simulate keyboard operations to upload files

#! Usr/bin/env python #-*-coding: UTF-8-*-"@ author: sleeping_cat @ Contact: zwy24zwy@163.com "# automatic attachment upload without manual intervention # simulate the keyboard operation to upload files from selenium import webdriverimport unittestimport timeimport tracebackimport win32clipboard as wimport win32apiimport win32confrom selenium. webdriver. support. ui import WebDriverWaitfrom selenium. webdriver. common. by import Byfrom selenium. webdriver. support import expected_conditions as ECfrom selenium. common. exceptions import TimeoutException, NoSuchElementException # used to set the clipboard content def setText (aString): w. openClipboard () w. emptyClipboard () w. setClipboardData (win32con. CF_UNICODETEXT, aString) w. closeClipboard () # keyboard key ing dictionary VK_CODE = {'enter': 0x0D, 'ctrl ': 0x11, 'V ': 0x56} # Press def keyDown (keyName) on the keyboard key: win32api. keybd_event (VK_CODE [keyName], 0) # keyboard key lift def keyUp (keyName): win32api. keybd_event (VK_CODE [keyName], 0, win32con. KEYEVENTF_KEYUP, 0) class TestDemo (unittest. testCase): def setUp (self): self. driver = webdriver. chrome () def test_uploadFileByKeyboard (self): url = 'e: // uploadFile.html 'self. driver. get (url) try: wait = WebDriverWait (self. driver, 10, 0.2) wait. until (EC. element_to_be_clickable (. ID, 'file') returns t TimeoutException as e: print (traceback. print_exc () failed t NoSuchElementException as e: print (traceback. print_exc () failed t Exception as e: print (traceback. print_exc () else: setText ('C: \ test.txt ') # Set the file name and path to be uploaded to the clipboard self. driver. find_element_by_id ('file '). click () time. sleep (3) # press Ctrl + V keyDown ('ctrl ') keyDown ('V') on the simulated keyboard # press Ctrl + V keyUp ('V') on the simulated keyboard ') keyUp ('ctrl ') time. sleep (1) keyDown ('enter') keyUp ('enter') time. sleep (2) fileSubmitButton = self. driver. find_element_by_id ('filesubmit ') time. sleep (2) fileSubmitButton. click () def tearDown (self): self. driver. quit () if _ name _ = '_ main _': unittest. main ()

3. Use third-party tools AutoIt to upload files

#! Usr/bin/env python #-*-coding: UTF-8-*-"@ author: sleeping_cat @ Contact: zwy24zwy@163.com "# automatically upload attachments without manual intervention # use a third-party tool AutoIt to upload files from selenium import webdriverimport unittest, time, OS, tracebackfrom selenium. webdriver. support. ui import WebDriverWaitfrom selenium. webdriver. common. by import Byfrom selenium. webdriver. support import expected_conditions as ECfrom selenium. common. exceptions import TimeoutException, NoSuchElementExceptionclass TestDemo (unittest. testCase): def setUp (self): self. driver = webdriver. chrome () def test_uploadFileByAutoIt (self): url = 'e: \ uploadFile.html 'self. driver. get (url) try: wait = WebDriverWait (self. driver, 10, 0.2) wait. until (EC. element_to_be_clickable (. ID, 'file') returns t TimeoutException as e: print (traceback. print_exc () failed t NoSuchElementException as e: print (traceback. print_exc () failed t Exception as e: print (traceback. print_exc () else: self. driver. find_element_by_id ('file '). click () OS. system ('d: \ iDownload \ test.exe'generated file time. sleep (3) fileSubmitButton = self. driver. find_element_by_id ('filesubmit ') fileSubmitButton. click () wait. until (EC. title_is ('file uploaded successfully') def tearDown (self): self. driver. quit () if _ name _ = '_ main _': unittest. main ()

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.