使用Robot类将图像上传到网站上的裁剪工具中。 VK_ENTER 未退出文件资源管理器

问题描述 投票:0回答:2

看到这篇文章时: 我得到了大部分解决方案,但最后的 VK_ENTER 没有带我返回网页,这导致了另一个问题:返回页面视图,而不是放置图像的裁剪工具。

有人可以让我知道我需要做什么才能让它发挥作用吗?谢谢!!

这是代码:

public static void setClipboardData(String string) {
   StringSelection stringSelection = new StringSelection(string);
   Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}
And that is what i do, just after opening the "open" window:

setClipboardData("C:\\path to file\\example.jpg");
//native key strokes for CTRL, V and ENTER keys
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);

这里是屏幕录制的链接,说明其工作原理(调整大小和移动与问题无关)上传个人资料图片]1

问题之一是 VK_ENTER 永远不会离开文件资源管理器并返回网页。如果我在测试运行时手动执行此操作,则永远找不到“保存”按钮。这是错误:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"/html/body/div[12]/form/div/div[1]/button[1]"}
Command duration or timeout: 30.10 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:59:12'
System info: host: 'Janet-PC', ip: '192.168.56.1', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_66'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=43.0.4, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: b7ceb493-af57-4290-a585-bc237e043848
*** Element info: {Using=xpath, value=/html/body/div[12]/form/div/div[1]/button[1]}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:647)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:353)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:490)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:345)
    at groupPhotoUploadTest.groupPhotoUploadTest.main(groupPhotoUploadTest.java:54)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"/html/body/div[12]/form/div/div[1]/button[1]"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:59:12'
System info: host: 'Janet-PC', ip: '192.168.56.1', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_66'
Driver info: driver.version: unknown
    at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/Janet/AppData/Local/Temp/anonymous8445833340663492444webdriver-profile/extensions/[email protected]/components/driver-component.js:10659)
    at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/Users/Janet/AppData/Local/Temp/anonymous8445833340663492444webdriver-profile/extensions/[email protected]/components/driver-component.js:621)

我尝试过 Actions 对象以及 JavascriptExecutor,但似乎都不起作用。

完整代码如下:

package groupPhotoUploadTest;


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;


public class groupPhotoUploadTest {
  private static WebDriver driver;
  private static String baseUrl;

public static void main(String[] args) throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://loadtest1.staging.brio.viddler.com/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.get(baseUrl + "/");
    driver.findElement(By.linkText("Login")).click();
    driver.findElement(By.name("email_address")).clear();
    driver.findElement(By.name("email_address")).sendKeys("[email protected]");
    driver.findElement(By.name("password")).clear();
    driver.findElement(By.name("password")).sendKeys("Annie0308!");
    driver.findElement(By.cssSelector("button.button")).click();
    driver.findElement(By.linkText("Load Test1")).click();
    driver.findElement(By.linkText("Manage Site")).click();
    driver.findElement(By.linkText("Manage")).click();
    driver.findElement(By.xpath("//div[@id='container']/div/table/tbody/tr[5]/td[6]/a")).click();
    driver.findElement(By.linkText("Upload Logo")).click();
    driver.findElement(By.name("image_file")).clear();
    driver.findElement(By.name("image_file")).click();     
//    driver.findElement(By.name("image_file")).sendKeys("C:\\Users\\Janet\\Pictures\\P8291157.JPG");
    setClipboardData("C:\\Users\\Janet\\Pictures\\P8291157.JPG");
    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);
 //   robot.delay(5000);
/*  WebElement saveLink = driver.findElement(By.xpath("/html/body/div[12]/form/div/div[1]/button[1]"));
    Actions actions = new Actions(driver);
    actions.moveToElement(saveLink);
    actions.click();
    actions.build().perform(); */
    JavascriptExecutor jse = (JavascriptExecutor)driver;
    WebElement saveLink = driver.findElement(By.xpath("/html/body/div[12]/form/div/div[1]/button[1]"));
    jse.executeScript("arguments[0].click();", saveLink);
 //   driver.findElement(By.cssSelector("button.submit.button")).click();
  }

public static void setClipboardData(String string) {
       StringSelection stringSelection = new StringSelection(string);
       Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
    }

  public void tearDown() throws Exception {
    driver.quit();
  }
}
java selenium-webdriver
2个回答
0
投票

终于可以使用了。这是完整的代码。必须使用 cssSelector 而不是 xpath:

  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); 
    Thread.sleep(3000);
    robot.keyRelease(KeyEvent.VK_ENTER); 
    Thread.sleep(3000);
    Thread.sleep(5000);
    WebElement saveLink = driver.findElement(By.cssSelector("button.submit.button"));
    Actions actions = new Actions(driver);
    actions.moveToElement(saveLink);
    actions.click();
    actions.build().perform();  
    Thread.sleep(5000);
    WebElement submitLink = driver.findElement(By.cssSelector("input.button"));
    actions.moveToElement(submitLink);
    actions.click();
    actions.build().perform();  

0
投票

尝试下面的代码,它会起作用

    uploadbutton = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(uploadphotoelement)));
    uploadbutton.click();
    //Set the file path to be uploaded using Robot class
    ss = new StringSelection(ProfileImagePath);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);

    robot = new Robot();

    if(driver instanceof FirefoxDriver) {
        // Press Enter to open the file upload dialog
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
    }

    // Wait for the file dialog to appear (time may vary)
    robot.delay(1000);


    // Paste the clipboard content (file path), I have uploaded the flower image from my locally saved pic.
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);


    // Press Enter to select the file
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
© www.soinside.com 2019 - 2024. All rights reserved.