使用Selenium自动化图像上传,但不使用AutoIt [英] Automate image upload with Selenium but without AutoIt

查看:79
本文介绍了使用Selenium自动化图像上传,但不使用AutoIt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我公司的网站上有一个非传统的图片上传按钮.我希望有一种使用此按钮的自动上传图像的方法,但不必使用AutoIt之类的工具即可与文件浏览器进行交互.

I have a non-traditional, image upload button on my company's website. I want to have an automated way to upload an image using this button, but without having to use a tool like AutoIt in order to interact with the file explorer.

以下是此按钮的HTML的示例:

Here's a sample of this button's HTML:

<button ng-click="onClick()" ng-disabled="readOnly" accepted-types="image/*" on-files-selected="onFilesSelected" allow-multiple="true" readonly="readonly">Add images</button>

与通常的 input 元素有点不同,例如< input type ="file"> ,并且它使用的是AngularJS.由于它不是 input 元素,所以我认为我无法使用Selenium的 sendKeys()函数在计算机上输入图像的文件位置.

It's a bit different than the usual input element, e.g. <input type="file">, and it's using AngularJS. Since it's not an input element, I don't think I can use Selenium's sendKeys() function to input the image's file location on my machine.

上传图片是否有任何破解或解决方法?我正在考虑改写 onClick()函数以从指定位置进行读取(这种方法似乎并不可行),或者可能是拦截了打开文件资源管理器并尝试运行的事件.从那里破解我的方法,但是这些都是解决问题的不受支持和未经测试的方法.

Is there any hack or workaround to uploading the image? I was considering things like overwriting the onClick() function to do read from a specified location (this approach doesn't really seem like it's doable), or possibly intercepting the event that opens the file explorer and trying to hack my way from there, but these are all just unsupported and untested approaches to solving the problem.

是否可以在另一个浏览器自动化工具(如Microsoft的Playwright)中进行此操作?

Would it be possible to do this in another browser-automation tool, like Microsoft's Playwright?

推荐答案

使用JACOB,它提供java本机接口,您可以在其中使用AutoIt功能和硒.这里是一个示例,我在MSTeams,Slack等大多数地方都在使用它自动化[上传功能]即可完成工作.

Use JACOB it provides java native interface where you can use AutoIt functionalities with selenium here is a sample I am using it in most of the places like MSTeams,Slack for Automation[Upload Feature] it does the job.

第1步:下载JACOB jar

Step 1: Download JACOB jar

第2步:注册AutoIt COM库,例如regsvr32 AutoItX3_x64.dll

Step 2: Register the AutoIt COM libraries e.g regsvr32 AutoItX3_x64.dll

  • jacob.jar
  • AutoItX4Java.jar
  • jacob-1.18-x64.dll
  • jacob-1.18-x86.dll

[此代码与文件浏览器交互]

[This Code Interacts with file explorer]


import com.jacob.com.LibraryLoader;

import autoitx4java.AutoItX;

public class Attachments {

   public void uploadAttachments(){

        File f = new File("Location");
        File[] fil =f.listFiles();

        //Upload Button Xpath
        WebElement uploadFromComp = driver.findElement(By.xpath("//span[contains(text(),'Upload from my computer')]"));
        uploadFromComp.click();
        Thread.sleep(5000);

        String jacobDllVersionToUse;
        if (jvmBitVersion().contains("32")) {
            jacobDllVersionToUse = "jacob-1.19-x86.dll";
        } else {
            jacobDllVersionToUse = "jacob-1.19-x64.dll";
        }

        File file1 = new File("registerAutoItDll", jacobDllVersionToUse);
        System.setProperty(LibraryLoader.JACOB_DLL_PATH, file1.getAbsolutePath());

        AutoItX x = new AutoItX();
        x.winWaitActive("Open");
        x.sleep(5000);
        x.send(fil[j].getAbsolutePath());
        x.send("{ENTER}", false);
}}

我希望它对您有用.

这篇关于使用Selenium自动化图像上传,但不使用AutoIt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆