Selenium---Python3---上传文件
#上传文件使用send_keys方法添加本地文件
<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>
#coding = utf-8 from selenium import webdriver import os,time #Selenium 下拉框处理 #D:\chromedriver_win32\chromedriver.exe class driver_1(object): #定义一个对象 def __init__(self): self.driver = webdriver.Chrome('D:\chromedriver_win32\chromedriver.exe') def driver_alert(self): file_path = 'file:///' + os.path.abspath('upload_file.html') self.driver.get(file_path) time.sleep(2) #定位上传按钮,添加本地文件 self.driver.find_element_by_name('file').send_keys('D:\\PycharmProjects1\\test.txt') time.sleep(3) #退出浏览器 self.driver.close() if __name__ == "__main__": a = driver_1() a.driver_alert()