今天看啥
    热点:

      天发国际娱乐官网:2004年,日本国会通过《内容促进法》,从国家层面大力支持发展动漫产业。

      无人工干预地自动上传附件,人工干预上传附件


      <html>
      <head>
          <title>上传文件</title>
          <meta http-equiv = "Content-Type" content = "text/html; charset = utf-8">
      </head>
      <body>
          <form enctype = "multipart/form-data" action = "parse_file.jsp" method="post">
              <p>Browse for a file to upload:</p>
              <input id = "file" name="file" typr="file"></input>
              <br/><br/>
              <input type="submit" id="filesubmit" value="SUBMIT"></input>
          </form>
      </body>
      </html>

      1、使用webdriver的send_keys方法上传文件

      #!usr/bin/env python  
      #-*- coding:utf-8 -*-  
      """ 
      @author:   sleeping_cat
      @Contact : zwy24zwy@163.com 
      """ 
      #无人工干预地自动上传附件
      #使用webdriver的send_keys方法上传文件
      
      from selenium import webdriver
      import unittest
      import time
      import traceback
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      from selenium.common.exceptions import TimeoutException,NoSuchElementException
      
      class 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)#创建一个显示等待对象
                  wait.until(EC.element_to_be_clickable((By.ID,'file')))#显示等待判断被测试页面上的上传文件按钮是否处于可被单击状态
              except TimeoutException as e:
                  print(traceback.print_exc())
              except NoSuchElementException as e:
                  print(traceback.print_exc())
              except Exception as e:
                  print(traceback.print_exc())
              else:
                  fileBox = self.driver.find_element_by_id('file')
                  fileBox.send_keys('c:\\test.txt')#在文件上传框的路径框里输入要上传的文件路径C://test.txt
                  time.sleep(3)
                  fileSubmitButton = self.driver.find_element_by_id('filesubmit')
                  fileSubmitButton.click()
      
          def tearDown(self):
              self.driver.quit()
      
      if __name__ == '__main__':
          unittest.main()

      2、模拟键盘操作,实现上传文件

      #!usr/bin/env python  
      #-*- coding:utf-8 -*-  
      """ 
      @author:   sleeping_cat
      @Contact : zwy24zwy@163.com 
      """ 
      #无人工干预地自动上传附件
      #模拟键盘操作,实现上传文件
      
      from selenium import webdriver
      import unittest
      import time
      import traceback
      import win32clipboard as w
      import win32api
      import win32con
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      from selenium.common.exceptions import TimeoutException,NoSuchElementException
      
      #用于设置剪贴板内容
      def setText(aString):
          w.OpenClipboard()
          w.EmptyClipboard()
          w.SetClipboardData(win32con.CF_UNICODETEXT,aString)
          w.CloseClipboard()
      
      #键盘按键映射字典
      VK_CODE = {
          'enter':0x0D,
          'Ctrl':0x11,
          'V':0x56}
      
      #键盘键按下
      def keyDown(keyName):
          win32api.keybd_event(VK_CODE[keyName],0,0,0)
      
      #键盘键抬起
      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((By.ID,'file')))
              except TimeoutException as e:
                  print(traceback.print_exc())
              except NoSuchElementException as e:
                  print(traceback.print_exc())
              except Exception as e:
                  print(traceback.print_exc())
              else:
                  setText('c:\\test.txt')#将即将要上传的文件名及路径设置到剪贴板中
                  self.driver.find_element_by_id('file').click()
                  time.sleep(3)
                  #模拟键盘按下Ctrl+V
                  keyDown('Ctrl')
                  keyDown('V')
                  #模拟键盘释放Ctrl+V
                  keyUp('V')
                  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、使用第三方工具AutoIt上传文件

      #!usr/bin/env python  
      #-*- coding:utf-8 -*-  
      """ 
      @author:   sleeping_cat
      @Contact : zwy24zwy@163.com 
      """ 
      #无人工干预地自动上传附件
      #使用第三方工具AutoIt上传文件
      
      from selenium import webdriver
      import unittest,time,os,traceback
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      from selenium.common.exceptions import TimeoutException,NoSuchElementException
      
      class 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((By.ID,'file')))
              except TimeoutException as e:
                  print(traceback.print_exc())
              except NoSuchElementException as e:
                  print(traceback.print_exc())
              except Exception as e:
                  print(traceback.print_exc())
              else:
                  self.driver.find_element_by_id('file').click()
                  os.system('d:\\iDownload\\test.exe')#通过Python提供的os模块的system方法执行生成的test.exe文件
                  time.sleep(3)
                  fileSubmitButton = self.driver.find_element_by_id('filesubmit')
                  fileSubmitButton.click()
                  wait.until(EC.title_is('文件上传成功'))
          def tearDown(self):
              self.driver.quit()
      
      if __name__ == '__main__':
          unittest.main()

      www.1click-soft.comtruehttp://www.1click-soft.com/Pythonjc/1284236.htmlTechArticle无人工干预地自动上传附件,人工干预上传附件 html head title 上传文件 / title meta http-equiv = "Content-Type" content = "text/html; charset = utf-8" / head...

      相关文章

      相关搜索: ftp空间

      帮客评论

      视觉看点
      百度 360 搜狗