selenium python (十四)上傳文件的處理


#!/usr/bin/python
# -*- coding: utf-8 -*-
__author__ = 'zuoanvip'

#上傳過程一般要打開一個系統的windows窗口,從窗口選擇本地文件添加。所以一般會卡在如何操作本地window窗口。解決的方法很簡單,只需要定位到上傳按鈕,然后通過send_keys添加本地文件路徑即可
from selenium import webdriver
import  os
import time

driver = webdriver.Firefox()

#打開上傳頁面
file_path = 'file:///'+os.path.abspath()
driver.get(file_path)

#定位上傳按鈕,添加本地文件
driver.find_element_by_name('file').send_keys('D:\\selenium_use_case\upload_file.text')

time.sleep(5)

==========================================================

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/bootstrapcombined.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/twitterbootstrap/2.3.2/js/bootstrap.min.js"></script>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM