在 Chrome 浏览器中使用 Robot 在 selenium 中上传不起作用
Uploading is not working in selenium using Robot in Chrome Broswer
我正在使用 Robot
class 上传文件,但无法正常工作。
正在显示文件资源管理器以供上传,但 Robot
class 不工作:
driver = new FirefoxDriver();
driver.get("http://www.toolsqa.com/automation-practice-form");
driver.manage().window().maximize();
driver.findElement(By.id("photo")).click;
Thread.sleep(2000);
StringSelection stringSelection = new StringSelection("C:\Users\Desktop\Bug\ui_1.png");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
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);
如何 Robot
使用 chrome 浏览器正常工作。面对 Firefox 的问题。有解决办法吗?
您实际上可以避免使用 Robot
并仅使用 selenium 来解决它。只需将密钥发送到文件输入,然后提交表单:
WebElement photo = driver.findElement(By.id("photo"));
photo.sendKeys("C:\Users\Desktop\Bug\ui_1.png");
// TODO: fill out other fields
// this would find the corresponding form and submit it
photo.submit();
等待几秒钟,然后按回车键。在您的代码中添加 robot.delay(3000);
,如下所示。这可能会解决问题。
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.delay(3000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
我正在使用 Robot
class 上传文件,但无法正常工作。
正在显示文件资源管理器以供上传,但 Robot
class 不工作:
driver = new FirefoxDriver();
driver.get("http://www.toolsqa.com/automation-practice-form");
driver.manage().window().maximize();
driver.findElement(By.id("photo")).click;
Thread.sleep(2000);
StringSelection stringSelection = new StringSelection("C:\Users\Desktop\Bug\ui_1.png");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
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);
如何 Robot
使用 chrome 浏览器正常工作。面对 Firefox 的问题。有解决办法吗?
您实际上可以避免使用 Robot
并仅使用 selenium 来解决它。只需将密钥发送到文件输入,然后提交表单:
WebElement photo = driver.findElement(By.id("photo"));
photo.sendKeys("C:\Users\Desktop\Bug\ui_1.png");
// TODO: fill out other fields
// this would find the corresponding form and submit it
photo.submit();
等待几秒钟,然后按回车键。在您的代码中添加 robot.delay(3000);
,如下所示。这可能会解决问题。
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.delay(3000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);