3 回答
TA贡献1865条经验 获得超7个赞
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 os中完美运行的代码..
TA贡献1828条经验 获得超4个赞
上传窗口是 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);
添加回答
举报