简体   繁体   English

如何在Java中使用Sikuli和Selenium Webdriver自动上传多个文件

[英]How to automate upload multiple files using sikuli and selenium webdriver in java

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

Please note that typically, you can automate file upload scenario using Selenium only, no need for Sikuli. 请注意,通常,您只能使用Selenium自动执行文件上传方案,而无需使用Sikuli。 For uploading a file you just need to call sendKeys() method (with file path as argument) on the WebElement that is displayed for file uploading. 要上传文件,您只需要在显示用于文件上传的WebElement上调用sendKeys()方法(以文件路径作为参数)即可。 Code goes like this: 代码如下:

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

And then click the upload button: 然后点击上传按钮:

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

Sikuli : 西库利:

I have used Sikuli to automate file download scenario in IE and below are the steps for this: 我已使用Sikuli在IE中自动执行文件下载方案,以下是此步骤:

  1. First capture image of Save button in File download dialog box and save it 首先在“文件下载”对话框中捕获“保存”按钮的图像并将其保存
  2. Put Sikuli jar in your Java project 将Sikuli jar放入Java项目
  3. Use following code snippet 使用以下代码段

// Code: //代码:

//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")
}

Thanks sandeep! 谢谢sandeep!

tried below script using Screen and Pattern class of Sikuli to capture desktop based file from open folder windows at run time and it works!! 尝试使用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..");
                     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM