![](/img/trans.png)
[英]Not able to upload file using Windows file upload popup, tried using Java Robot class
[英]Not able to upload file using robot class and Sendkeys
我無法使用機器人 class 和 sendkeys 上傳文件。
下面是我試過的代碼
package garbage;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class imageupload {
static WebDriver driver;
static String baseURL="http://elance.wetwaresoft.com/account/register";
public static void main(String[] args) throws AWTException, InterruptedException, IOException
{
driver=new FirefoxDriver();
driver.get(baseURL);
WebElement btn=driver.findElement(By.xpath("//*[@class='fileupload-new']"));
btn.click();
System.out.println("Going in Robot class");
Runtime.getRuntime().exec("notepad");
Robot r=new Robot();
r.delay(1000);
r.keyPress(KeyEvent.VK_ENTER);
r.keyPress(KeyEvent.VK_D);
r.keyPress(KeyEvent.VK_SHIFT);
r.keyPress(KeyEvent.VK_SEMICOLON);
r.keyRelease(KeyEvent.VK_SHIFT);
r.keyPress(KeyEvent.VK_BACK_SLASH);
r.keyPress(KeyEvent.VK_A);
r.keyPress(KeyEvent.VK_DOWN);
r.keyPress(KeyEvent.VK_ENTER);
System.out.println("File uploaded");
}
}
當我在記事本中執行我的機器人 class 代碼時,它會輸入路徑,但是當我在 window 彈出窗口中執行它時,它不會輸入任何內容。
如何使用 robot 和 sendkeys 上傳文件?
我已經使用Robot將文件upalod邏輯分離到單獨的類中,並使方法靜態化。 在fileAttachmentUsingRobot
方法中,將路徑傳遞到要上傳的圖像,即圖像的完整系統路徑。
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
/**
* Created by ypolshchykau on 30.01.2015.
*/
public class FileAttacherRobotImplementation {
public final static Logger log = LoggerFactory.getLogger(FileAttacherRobotImplementation.class);
/**
* this method implements file attachment using Robot mechanism
*
* @param filePathToImage
*/
public static void fileAttachmentUsingRobot(String filePathToImage) {
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
setClipboardData(filePathToImage);
robot.delay(1000);
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);
}
/**
* method for copying file in order to attach file in File open window
*
* @param str
*/
public static void setClipboardData(String str) {
StringSelection stringSelection = new StringSelection(str);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}
}
希望這對您有所幫助。
String file = "C:\\Users\\KT System\\Downloads\\sample1.xlsx";
StringSelection stringselection = new StringSelection(file);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringselection, null);
Robot robot = new Robot();
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_CONTROL);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_V);
Thread.sleep(2000);
robot.keyRelease(KeyEvent.VK_CONTROL);
Thread.sleep(2000);
robot.keyRelease(KeyEvent.VK_V);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(2000);
robot.keyRelease(KeyEvent.VK_ENTER);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.