![](/img/trans.png)
[英]Is there any way to handle authentication alert using selenium without any OS tool like autoIT and robot
[英]Handling OS windows in Ubuntu without using Robot class (Selenium)
我正在從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>
不應使用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();
}
解決問題的常用方法是使用
WebElement file_input = driver.findElement(By.id("category_tile_upload"));
file_input.sendKeys("/path/to/file/to/upload");
實施這些代碼行的結果是什么?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.