如何在java中使用sikuli和selenium webdriver自动上传多个文件

时间:2015-07-14 07:46:02

标签: selenium-webdriver selenium-chromedriver sikuli-script

我是sikuli的新手,无法为Web应用程序生成用于上传功能的Sikuli脚本。

2 个答案:

答案 0 :(得分:0)

请注意,通常情况下,您只能使用Selenium自动执行文件上传方案,不需要Sikuli。 要上传文件,您只需要在显示文件上载的WebElement上调用sendKeys()方法(文件路径为参数)。代码是这样的:

//Put this for textbox near to upload button
driver.findElement(By.id("id_or_other_locator_goes_here")).sendKeys("file_path_goes_here");

然后点击上传按钮:

driver.findElement(By.xpath("locator_for_upload_button")).click(); // Click Upload button

Sikuli:

我已经使用Sikuli在IE中自动执行文件下载方案,以下是步骤:

  1. 首先在“文件下载”对话框中捕获“保存”按钮的图像并保存
  2. 将Sikuli jar放入Java项目
  3. 使用以下代码段
  4. //代码:

    //Save the file in Downloads directory by using on Sikuli
    
    ScreenRegion s = new DesktopScreenRegion();
    Target target = new ImageTarget(new File("SavedImagePath.png"));
    ScreenRegion r = s.find(target);
    Mouse mouse = new DesktopMouse();   
    if (r != null) {
        mouse.click(r.getCenter());
        Thread.sleep(5000);
    } else {
        System.out.println("Unable to click using Sikuli")
    }
    

答案 1 :(得分:0)

谢谢sandeep!

使用Sikuli的Screen和Pattern类在脚本下尝试在运行时从打开的文件夹窗口捕获基于桌面的文件,它可以正常运行!!

                     String FileToUpload = "/location of file to upload/"
                     String fileNameLoc = "/fileName_input sikuli image location"
                     String openButtonLoc = "/Open button sikuli image location/"

                     //Code to perform action using action using sikuli script
                     Screen src = new Screen();
                     src.setAutoWaitTimeout(80);
                     Pattern fileName = new Pattern(fileNameLoc).similar((float) 0.5);
                     if (src.exists(fileName, 10) != null)
                     {
                          System.out.println("File Name Pattern exist..");
                          Match match = src.getLastMatch();
                          match.find(fileName);
                          match.click(fileName);
                          match.type(fileName, FileToUpload);
                          match.setAutoWaitTimeout(50);
                     }
                     else
                     {
                          System.out.println("File Name pattern not found on screen..");
                     }

                     Pattern open = new Pattern(openButtonLoc).similar((float) 0.5);
                     if (src.exists(open, 5) != null)
                     {
                          System.out.println("Open Button pattern exist..");
                          Match match = src.getLastMatch();
                          match.find(open);
                          match.click(open);
                          match.setAutoWaitTimeout(30);
                     }
                     else
                     {
                          System.out.println("Open buton pattern not found on screen..");
                     }