[英]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. 当看这篇文章时:我得到了大部分的解决方案,但是最后的VK_ENTER并没有带我回到网页,这又导致了另一个问题,即回到了页面视图,而不是回到放置图像的裁剪工具。 。
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 这是指向屏幕记录的链接,该记录应如何工作(调整大小和移动与该问题无关)[上传个人资料图片] 1
One of the issues is that the VK_ENTER is never done to leave file explorer and return to the web page. 问题之一是VK_ENTER永远不会离开文件资源管理器并返回到网页。 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. 我已经尝试了Actions对象以及JavascriptExecutor,但似乎都没有用。
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:
必须使用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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.