Skip to content

Latest commit

 

History

History
49 lines (40 loc) · 1.38 KB

upload_file.md

File metadata and controls

49 lines (40 loc) · 1.38 KB

上传文件

场景

上传文件的方法是找到上传文件的对象,通常是的对象。然后直接往这个对象send_keys,传入需要上传文件的正确路径。绝对路径和相对路径都可以,但是上传的文件必须存在,否则会报错。

代码

upload_file.html

  <html>
    <head>
      <meta http-equiv="content-type" content="text/html;charset=utf-8" />
      <title>upload_file</title>		
      <script type="text/javascript" async="" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
      <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" />		
      <script type="text/javascript">
      </script>
    </head>
      
    <body>
      <div class="row-fluid">
        <div class="span6 well">		
          <h3>upload_file</h3>
          <input type="file" name="file" />
        </div>		
      </div>		
    </body>
    <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
  </html>

upload_file.rb

  #encoding: utf-8
  require 'selenium-webdriver'

  dr = Selenium::WebDriver.for :chrome
  file_path = 'file:///' + File.expand_path(File.join('.', 'upload_file.html'))
  dr.get file_path

  dr.find_element(name: 'file').send_keys('./upload_file.md')

  sleep(2)
  dr.quit()