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

<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 功能时,它会打开上传窗口,但不会选择文件。还有其他方法吗?


芜湖不芜
浏览 257回答 3
3回答

莫回无

File file = new File("/users/chennai4/downloads/lead.csv");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StringSelection stringSelection= new StringSelection(file.getAbsolutePath());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Copy to clipboard&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Robot robot = new Robot();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Cmd + Tab is needed since it launches a Java app and the browser looses focus&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; robot.keyPress(KeyEvent.VK_META);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; robot.keyPress(KeyEvent.VK_TAB);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; robot.keyRelease(KeyEvent.VK_META);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; robot.keyRelease(KeyEvent.VK_TAB);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; robot.delay(500);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Open Goto window&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; robot.keyPress(KeyEvent.VK_META);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; robot.keyPress(KeyEvent.VK_SHIFT);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; robot.keyPress(KeyEvent.VK_G);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; robot.keyRelease(KeyEvent.VK_META);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; robot.keyRelease(KeyEvent.VK_SHIFT);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; robot.keyRelease(KeyEvent.VK_G);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Paste the clipboard value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; robot.keyPress(KeyEvent.VK_META);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; robot.keyPress(KeyEvent.VK_V);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; robot.keyRelease(KeyEvent.VK_META);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; robot.keyRelease(KeyEvent.VK_V);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Press Enter key to close the Goto window and Upload window&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; robot.keyPress(KeyEvent.VK_ENTER);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; robot.keyRelease(KeyEvent.VK_ENTER);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; robot.delay(1000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; robot.keyPress(KeyEvent.VK_ENTER);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; robot.keyRelease(KeyEvent.VK_ENTER);这是我在mac os中完美运行的代码..

眼眸繁星

我认为你必须按回车,即使用 sendKeys 发送回车键

明月笑刀无情

上传窗口是 Windows 弹出窗口,而不是浏览器弹出窗口,因此 selenium 命令在这种情况下不起作用。您可以使用 java Robot 和 StringSelection 类。第一步:将文件路径复制到系统剪贴板第二步:将文件路径粘贴到上传窗口(发送键Ctrl+V),然后发送回车键。添加以下软件包import java.awt.Robot;import java.awt.Toolkit;import java.awt.datatransfer.StringSelection;import java.awt.event.KeyEvent;使用 StringSelection 类进行复制和粘贴操作。StringSelection stringSelection = new StringSelection("\\Users\\nilaapps13\\Desktop\\lead.csv");Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);使用 Robot 类发送键盘事件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);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java