简体   繁体   English

如何使用Selenium专注于文件对话框来选择文件

[英]How to focus on file dialog box using Selenium to pick a file

I wish to upload a file into a specific site I'm supposed to test.我希望将文件上传到我应该测试的特定站点。 How do I make the Selenium focus on the file dialog box?如何让 Selenium 专注于文件对话框?

When I try to send keyboard strokes - the strokes go to the site, and not to the new file dialog box.当我尝试发送键盘笔画时 - 笔画会发送到站点,而不是发送到新文件对话框。

My code:我的代码:

 Actions builder = new Actions(browser);       
    builder.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).sendKeys(Keys.BACK_SPACE);
    builder.sendKeys(keyBoardText).perform();

There are several ways to upload files.上传文件有多种方式。 Usually we upload the files without clicking on upload button on the website and open the files dialog.通常我们上传文件而不点击网站上的上传按钮并打开文件对话框。

In Selenium you need to find the upload element on the site page and send the local file location.在 Selenium 中,您需要在站点页面上找到上传元素并发送本地文件位置。

driver.findElement(By.id("Upload Element")).sendKeys("PathToFile");

If you prefer to open the file dialog then I suggest you to use Robot framework.如果您更喜欢打开文件对话框,那么我建议您使用 Robot 框架。

   Robot robot = new Robot();
   robot.keyPress(KeyEvent.VK_ENTER);
   robot.keyRelease(KeyEvent.VK_ENTER);
   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);

But please mind, when you use the Robot framework you need to release the mouse cuz it holds the mouse cursor.但请注意,当您使用 Robot 框架时,您需要释放鼠标,因为它固定着鼠标光标。

Here you can find different ways for uploading files. 在这里您可以找到上传文件的不同方式。

Today I learned that I can only use Actions.sendKeys() on a WebElement that I clicked on.今天我了解到我只能在我点击的 WebElement 上使用 Actions.sendKeys()。

If I just need to click on the keyboard - I need to use the Robot object.如果我只需要点击键盘 - 我需要使用 Robot 对象。

This code solved my issue:这段代码解决了我的问题:

    softAsserter = new SoftAsserter(testParameters);
    String textToType = testParameters.get("actionData");
    Robot robot = null;
    try {
        robot = new Robot();
        for (char c : textToType.toCharArray()) {
            int keyCode = KeyEvent.getExtendedKeyCodeForChar(c);
            if (KeyEvent.CHAR_UNDEFINED == keyCode) {
                logger.error("Key code not found for character '" + c + "'");
            }
            else {
                try {
                    robot.keyPress(keyCode);
                    robot.delay(10);
                    robot.keyRelease(keyCode);
                    robot.delay(10);
                }
                catch (Exception e) {
                   if (c=='_') {
                       robot.keyPress(KeyEvent.VK_SHIFT);
                       robot.keyPress(KeyEvent.VK_MINUS);
                       robot.keyRelease(KeyEvent.VK_MINUS);
                       robot.keyRelease(KeyEvent.VK_SHIFT);
                   }
                   if (c==':') {
                       robot.keyPress(KeyEvent.VK_SHIFT);
                       robot.keyPress(KeyEvent.VK_SEMICOLON);
                       robot.keyRelease(KeyEvent.VK_SEMICOLON);
                       robot.keyRelease(KeyEvent.VK_SHIFT);
                   }
                }
            }
        }
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyPress(KeyEvent.VK_ENTER);
    }
    catch (Exception ex) {
        logger.error(ex.getMessage());
    }

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

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