Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Selenium上载图像文件_Java_Selenium_Automation - Fatal编程技术网

Java 使用Selenium上载图像文件

Java 使用Selenium上载图像文件,java,selenium,automation,Java,Selenium,Automation,我想通过点击浏览按钮上传一张图片,但有一个问题我无法处理窗口,因为Selenium没有提供任何访问权限。所以我使用了机器人的概念,但是不能点击我想要选择的图像 WebElement Account_logo = adriver.findElement(By.className("input-group-btn")); Account_logo.click(); StringSelection ss = new StringSelection("//C:\\Users\\romit\\Deskto

我想通过点击浏览按钮上传一张图片,但有一个问题我无法处理窗口,因为Selenium没有提供任何访问权限。所以我使用了机器人的概念,但是不能点击我想要选择的图像

WebElement Account_logo = adriver.findElement(By.className("input-group-btn"));
Account_logo.click();
StringSelection ss = new StringSelection("//C:\\Users\\romit\\Desktop\\LOGO.jpgg");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);


Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
另一种方法(对我们来说是可靠的)是创建AutoIt脚本,将其转换为exe文件并按如下方式使用:

Java代码: 单击打开按钮。au3: SetFocusToFileNameField.au3:
错误日志中有什么?我可以看出,这可能是文件扩展名LOGO.jpgg中的打字错误。您是否尝试使用sendKeys()将图像路径发送到浏览按钮?
// AutoIt script to set focus to file name field in browse window dialog
try {
    Runtime.getRuntime().exec(ConfigProperties.getBaseDirectory() + "\\conf\\windows\\autoit\\SetFocusToFileNameField.exe");
} catch (IOException e) {
    logger.info("Couldn't execute or set focus to file name field using AutoIt script: " + e.toString());
}
SleepUtil.sleepSmall();

// Imitate mouse events like ENTER, CTRL+C, CTRL+V
Robot robot;
try {
    robot = new Robot();
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    SleepUtil.sleepMedium();

} catch (AWTException e) {
    e.printStackTrace();
}

// AutoIt script to click to open button to attach file
try {
    Runtime.getRuntime().exec(ConfigProperties.getBaseDirectory() + "\\conf\\windows\\autoit\\ClickToOpenButton.exe");
    SleepUtil.sleepLongMedium();
} catch (IOException e) {
    logger.info("Couldn't click to open button to attach file using AutoIt script: " + e.toString());
}
; Click to Open button in File Explorer dialog

ClickToOpenButton()

Func ClickToOpenButton()
   Local $windowHandle;
   Local $windowTitle;
   Local $openButton = "Button1";

   If WinExists("File Upload") Then
      $windowTitle = "File Upload";
   Else
      $windowTitle = "Open";
   EndIf

   $windowHandle = WinWait($windowTitle, "");
   If Not WinActive($windowHandle) Then WinActivate($windowHandle);
   ControlClick($windowHandle, "", $openButton);

   ; Local $openButtonHandle;
   ; Local $openButtonPosition;
   ; $openButtonHandle = ControlGetHandle($windowTitle, "", $openButton);
   ; $openButtonPosition = WinGetPos($openButtonHandle);
   ; MouseClick($MOUSE_CLICK_PRIMARY, $openButtonPosition[0]+20, $openButtonPosition[1]+10, 2);
EndFunc
; Set focus to file name field in File Explorer dialog

SetFocusToFileNameField()

Func SetFocusToFileNameField()
   Local $windowHandle;
   Local $windowTitle;
   Local $windowField = "Edit1";

   If WinExists("File Upload") Then
      $windowTitle = "File Upload";
   Else
      $windowTitle = "Open";
   EndIf

   $windowHandle = WinWait($windowTitle, "");
   If Not WinActive($windowHandle) Then WinActivate($windowHandle);
   ControlSetText($windowHandle, "", "Edit1", "");
   ControlFocus($windowTitle, "", $windowField);
EndFunc
adriver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
StringSelection ss = new 
StringSelection("C:\\Users\\romit\\Desktop\\ELDEN\\LOGO.jpg");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
  Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_V);
  Thread.sleep(2000);
robot.keyRelease(KeyEvent.VK_V);
  Thread.sleep(2000);
robot.keyRelease(KeyEvent.VK_CONTROL);
  Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_ENTER);
  Thread.sleep(2000);
robot.keyRelease(KeyEvent.VK_ENTER);
  Thread.sleep(2000);