简体   繁体   English

在Mac OS上的文件选择器对话框窗口的帮助下,我无法使用Java打开文件

[英]I can't open file using java with the help of file chooser dialog window on Mac OS

Here is the code which I used on Windows OS. 这是我在Windows OS上使用的代码。

        String fileToUpload = "\"C:\\development\\projects\\GMailTAbleTest\\1.xlsx\"";
    WebElement uploadButton = driver.findElement(By.xpath("html/body/div[16]/div[2]"));
    uploadButton.click(); // This code find and click on button which open file chooser dialog window

StringSelection somestring = new StringSelection(fileToUpload);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(somestring, null);
        Robot robot = new Robot();
        robot.delay(3000);
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        TimeUnit.SECONDS.sleep(2);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER); 

On Mac OS I tried to use VK_META (to simulate a command button) instead of VK_CONTROL. 在Mac OS上,我尝试使用VK_META(模拟命令按钮)而不是VK_CONTROL。 I am using the corresponding UNIX path: 我正在使用相应的UNIX路径:

String s ="\"/Users/Yevhenii/Documents/workspace/TableTEstGmail/1.xlsx\"";

Please help me, I have used a Mac for only a few weeks. 请帮助我,我只用Mac几个星期。 I have tried to find the solution on the internet before wrote my question here. 在这里写下我的问题之前,我尝试过在互联网上找到解决方案。 But I found no solution. 但是我没有找到解决办法。

The problem was in a following: mac os have another principle of opening files from open dialog instead of windows. 问题出在以下方面:Mac OS具有从打开的对话框而不是Windows打开文件的另一原理。 And as for me the easiest way to solve the problem is using robot. 对我来说,解决问题的最简单方法是使用机器人。 So, when we click on our web page button which open, open dialog, we need select them. 因此,当我们单击打开的网页按钮,打开对话框时,我们需要选择它们。 And we use for this key combination COMMAND + TAB. 我们使用此键组合COMMAND + TAB。 Realization code are following: 实现代码如下:

String filePath = "/Users/Yevhenii/Documents/workspace/TableTEstGmail/2.xlsx";
StringSelection somestring = new StringSelection(filePath);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(somestring, null);
        Robot robot = new Robot();
        robot.delay(3000);

        robot.keyPress(KeyEvent.VK_META);
        robot.keyPress(KeyEvent.VK_TAB);
        robot.delay(90);
        robot.keyRelease(KeyEvent.VK_TAB);
        robot.keyRelease(KeyEvent.VK_META);
            TimeUnit.SECONDS.sleep(1); 

Then we need to find our file by path. 然后,我们需要按路径查找文件。 So when we press SHIFT + COMMAND + G it will opened window with the help of which we can open file by path. 因此,当我们按SHIFT + COMMAND + G时,它将打开一个窗口,借助它我们可以按路径打开文件。 Realization code: 实现代码:

 robot.keyPress(KeyEvent.VK_SHIFT);
        robot.keyPress(KeyEvent.VK_META);
        robot.keyPress(KeyEvent.VK_G);
        robot.keyRelease(KeyEvent.VK_G);
        robot.keyRelease(KeyEvent.VK_META);
        robot.keyRelease(KeyEvent.VK_SHIFT);
        TimeUnit.SECONDS.sleep(1);

The next step is to paste path to our file and it will have following representation: 下一步是将路径粘贴到我们的文件,它将具有以下表示形式:

            robot.keyPress(KeyEvent.VK_META);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_META);
        TimeUnit.SECONDS.sleep(1);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);   
        TimeUnit.SECONDS.sleep(2);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER); 

Why we press ENTER(RETURN) two times ? 为什么我们按两次ENTER(RETURN)键? First press accept inputed path and second press accepted selected file) 第一次按接受输入的路径,第二次按接受选择的文件)

I really kill many time for solving this problem, maybe the reason, i new on mac os. 我真的花了很多时间解决这个问题,也许是原因,我在Mac OS上是新手。 I hope this information will be interesting and important for you. 希望这些信息对您来说很有趣并且很重要。 )) ))

Regards Yevhenii 关于Yevhenii

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

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