简体   繁体   中英

Using Robot class to upload an image onto a website into a cropper tool. VK_ENTER not exiting file explorer

When looking at this post: I got most of my solution but the VK_ENTER at the end did not take me back to the web page which leads to another issue of getting back into view of the page and not the cropper tool where the image is placed.

Can someone let me know what I need to do to get this to work?? Thanks!!

Here is the code:

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);

Here is a link to a screen recording of how this is supposed to work(resizing and moving is not relevant to the issue)Upload profile picture] 1

One of the issues is that the VK_ENTER is never done to leave file explorer and return to the web page. If I do it manually while the test is running the Save button is never found. Here is the error for that:

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/fxdriver@googlecode.com/components/driver-component.js:10659)
    at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/Users/Janet/AppData/Local/Temp/anonymous8445833340663492444webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:621)

I have tried the Actions object as well as JavascriptExecutor and neither seem to work.

Here is the complete code:

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("jfrank@viddler.com");
    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();
  }
}

Finally have it working. Here is the complete code. Had to use the cssSelector and not the 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();  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM