【问题标题】:Uploading an image file using Selenium使用 Selenium 上传图像文件
【发布时间】:2018-04-16 11:09:15
【问题描述】:

我想通过单击浏览按钮来上传图像,但问题是我无法处理窗口,因为 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);

【问题讨论】:

  • 错误日志中有什么内容?我可以看到,这可能是文件扩展名中的拼写错误:LOGO.jpgg
  • 您是否尝试使用 sendKeys() 将图像的路径发送到浏览按钮?

标签: java selenium automation


【解决方案1】:

另一种方法(对我们来说可靠)是创建 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

【讨论】:

    【解决方案2】:
    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);
    

    【讨论】:

      猜你喜欢
      • 2016-10-19
      • 1970-01-01
      • 2022-11-25
      • 2014-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-17
      相关资源
      最近更新 更多