(九)上传文件

对于web页面的上传功能一般会有以下几种方式

普通上传:普通的附件上传都是将本地的路径作为一个值放input标签中,通过form表单提交的时候将这个值提交给服务器

插件上传:一般是指基于Flash与JavaScript或Ajax等技术所实现的上传功能或插件

一、sendKeys实现上传

对于通过input标签实现的上传,可以将其看作一个输入框,通过sendKeys()传入本地文件路径从而模拟上传功能

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>upload_file</title>
<link href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css"
rel="stylesheet" />
</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://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.js"></script>
</html>

 

System.setProperty("webdriver.firefox.bin", "D:/Program Files (x86)/Mozilla Firefox/firefox.exe");
		System.setProperty("webdriver.gecko.driver", "E://selenium//geckodriver-v0.24.0-win64//geckodriver.exe");
		WebDriver driver=new FirefoxDriver();
		
		driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
		driver.get("file:///E:/javaworkspace/Selenium_learn/txt/upfile.html");
		
		driver.findElement(By.name("file")).sendKeys("C:\\Users\\ASUS\\Desktop\\upfile.txt");

 

posted @ 2019-05-03 19:33  测试开发分享站  阅读(90)  评论(0编辑  收藏  举报