我正在从Jenkins运行我的selenium脚本,这些脚本在执行时运行在专门用于Selenium Tests的机器上。但我有个问题。下面是我使用Robot类执行的一些操作:
1)点击图片上传图标。2)打开与Ubuntu OS相关的窗口(文件上传)。3)我必须经过图像的位置。4)并点击打开。
但由于Robot类的限制,在远程机器上失败。
有没有人能帮我克服这个问题?

<div class="dropify-wrapper">
<div class="dropify-message">
<span class="file-icon"/>
<p>Drag and drop a file here or click</p>
<p class="dropify-error">Sorry, this file is too large</p>
</div>
<input id="category_tile_upload" class="dropify" data-default-file="" type="file"/>
<button class="dropify-clear" type="button">Remove</button>
<div class="dropify-preview">
<span class="dropify-render"/>
<div class="dropify-infos">
<div class="dropify-infos-inner">
<p class="dropify-filename">
<span class="file-icon"/>
<span class="dropify-filename-inner"/>
</p>
<p class="dropify-infos-message">Drag and drop or click to replace</p>
</div>
</div>
</div>
</div>
发布于 2016-11-22 22:42:24
这不应该使用Robot类来完成,因为可以使用Selenium来解决。不要单击打开上载窗口的元素,而是使用:
如果您的输入有一个ID,最好将它与By.id("inputID")一起使用,否则:
WebElement inputElement = driver.findElement(By.cssSelector("input[type='file']"));
element.sendKeys("/full/path/to/your/file");并通过以下两种方式获取路径:
String pathToFile = System.getProperty("user.dir") + "/src/resources/your.file; 要使用getResourceAsStream(),您可以:
try (InputStream in = this.getClass().getResourceAsStream(fileName)) {
return getStringFromInputStream(in);
} catch (IOException e) {
// handle
}
private String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}发布于 2016-11-23 17:08:37
解决问题的常用方法是使用
WebElement file_input = driver.findElement(By.id("category_tile_upload"));
file_input.sendKeys("/path/to/file/to/upload");实现这些代码行的结果是什么?
https://stackoverflow.com/questions/40740153
复制相似问题