Selenium自動化測試-進階
阿新 • • 發佈:2019-02-07
// 指定圖片的路徑
StringSelection sel = new StringSelection("C:\\Penguins.jpg");
// 把圖片檔案路徑複製到剪貼簿
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(sel,null);
System.out.println("selection" +sel);
// 點選本地上傳圖片
driver.findElement(By.xpath("//*[@id=\"app\"]/div/section/div[3]/input")).click();
// 新建一個Robot類的物件
Robot robot = new Robot();
Thread.sleep(1000);
// 按下回車
robot.keyPress(KeyEvent.VK_ENTER);
// 釋放回車
robot.keyRelease(KeyEvent.VK_ENTER);
// 按下 CTRL+V
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
// 釋放 CTRL+V
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
Thread.sleep(1000);
// 點選回車 Enter
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
StringSelection sel = new StringSelection("C:\\Penguins.jpg");
// 把圖片檔案路徑複製到剪貼簿
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(sel,null);
System.out.println("selection" +sel);
// 點選本地上傳圖片
driver.findElement(By.xpath("//*[@id=\"app\"]/div/section/div[3]/input")).click();
// 新建一個Robot類的物件
Robot robot = new Robot();
Thread.sleep(1000);
// 按下回車
robot.keyPress(KeyEvent.VK_ENTER);
// 釋放回車
robot.keyRelease(KeyEvent.VK_ENTER);
// 按下 CTRL+V
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
// 釋放 CTRL+V
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
Thread.sleep(1000);
// 點選回車 Enter
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);