使用Selenium上传图像文件

时间:2018-04-16 11:09:15

标签: java selenium automation

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

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);

2 个答案:

答案 0 :(得分:0)

替代方式(对我们来说可靠)是创建AutoIt脚本,转换为exe文件并使用如下所示:

Java代码:

// 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());
}

ClickToOpenButton.au3:

; 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

SetFocusToFileNameField.au3:

; 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

答案 1 :(得分:0)

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);