gpt4 book ai didi

java - 在java中使用selenium包含或上传文件

转载 作者:行者123 更新时间:2023-12-02 10:20:58 26 4
gpt4 key购买 nike

<input type="button" class="button white-button custom-modal-button" id="btnAttachment" ng-click="openAttachment()" value="Import CSV template">

enter code here

WebElement browse =driver.findElement(By.xpath("//*[@id=\"btnAttachment\"]"));
//pass the path of the file to be uploaded using Sendkeys method
browse.sendKeys("\\Users\\nilaapps13\\Desktop\\lead.csv");

当使用sendkeys功能时,它会打开上传窗口,但不会选择文件。还有其他办法吗?

最佳答案

File file = new File("/users/chennai4/downloads/lead.csv");
StringSelection stringSelection= new StringSelection(file.getAbsolutePath());
//Copy to clipboard
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
Robot robot = new Robot();
// Cmd + Tab is needed since it launches a Java app and the browser looses focus
robot.keyPress(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_META);
robot.keyRelease(KeyEvent.VK_TAB);
robot.delay(500);

//Open Goto window
robot.keyPress(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_G);
robot.keyRelease(KeyEvent.VK_META);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.keyRelease(KeyEvent.VK_G);

//Paste the clipboard value
robot.keyPress(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_META);
robot.keyRelease(KeyEvent.VK_V);

//Press Enter key to close the Goto window and Upload window
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.delay(1000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

这是我使用的代码,在 Mac 操作系统中完美运行..

关于java - 在java中使用selenium包含或上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54342231/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com