Selenium的文件上传JAVA脚本

在写文件上传脚本的时候,遇到了很多问题,包括元素定位,以及上传操作,现在总结下来以下几点:

1. 上传的控件定位要准确,必要时要进行等待

 WebElement adFileUpload = driver.findElement(By.xpath("//input[@type='file']"));

2. 上传操作

String filePath ="D:\\taxonomy\\DB\\HaierTaxonomy.xlsx";
    adFileUpload.sendKeys(filePath);
    try {
     Thread.sleep(10000);
     } catch (InterruptedException e) {
     e.printStackTrace();
     }
    driver.findElement(By.xpath("//button[@class='btn btn-primary start'][@type='button']")).click();
    driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
    driver.findElement(By.xpath("//input[@id='complete-template-upload'][@type='button']")).click();

3. 如上脚本,文件的路径一定要准确,否则即使脚本不出错,文件仍旧上传失败。

posted @ 2015-05-04 14:41  AmySu  阅读(190)  评论(0编辑  收藏  举报