有人可以帮助我如何使用selenium2library和Robot Framework从windows目录上传文件。我尝试在selenium2library中使用Choose File命令,但由于本地文件系统中不存在File,我收到错误。我不确定是否不考虑目录路径或任何其他问题。请给我有效的代码或任何替代解决方案。任何帮助将不胜感激。以下是我试过的命令
Choose file xpath = //input[@firmware-upgrade='firmware'] /Downloads/Cambium_Builds/Falcon/ePMP1000-Hotspot-2.5.1-b3.tar
HTML标记是“
答案 0 :(得分:5)
感谢每一个人的答案和时间,但幸运的是,下载脚本帮助我从Windows上传文件并且工作正常。使用AutoIT可能有不同的选项来执行相同的操作,但我已经尝试了我所知道的任何内容。
***Settings***
Library Selenium2Library
*** Variables ***
${Browser_Chrome} Chrome
${Device_77_URL} https://10.110.0.17
${Device_Path} C:\\Users\\mra001\\Downloads\\Builds\\Gambit\\500-2.5.1-b3.img
*** Keywords ***
Software Update
Open Browser ${Device_77_URL} Chrome
Input Text id=Username admin
Input Text password=Password admin
Click Button xpath = //button[@type='submit']
Wait Until Element Is Visible //a[text()='Home'] 20
Click Link xpath = //a[text()='Operations ']
Input Text xpath = //input[@firmware-upgrade='firmware'] ${Device_Path}
Click Button Upgrade Firmware
Sleep 70
Confirm Action
Close Browser
答案 1 :(得分:0)
单击浏览按钮以使用Robot Framework打开弹出窗口后,您可以执行以下操作来提交文件。
public static void robotwrite() throws Exception{
try{
RobotWrite rw = new RobotWrite();
rw.type("C:\\Desktop\\Example.docx");
Robot r = new Robot();
r.keyPress(VK_ENTER);
r.keyRelease(VK_ENTER);
}catch (Exception e){
Log.error("Could not write");
throw(e);
}
}
机器人类
import static java.awt.event.KeyEvent.*;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class RobotWrite {
private Robot robot;
public RobotWrite() throws AWTException {
this.robot = new Robot();
}
public void type(CharSequence characters) throws AWTException {
int length = characters.length();
for (int i = 0; i < length; i++) {
char character = characters.charAt(i);
type(character);
}
}
public void type(char character) throws AWTException {
switch (character) {
case 'a': doType(VK_A); break;
case 'b': doType(VK_B); break;
case 'c': doType(VK_C); break;
case 'd': doType(VK_D); break;
case 'e': doType(VK_E); break;
case 'f': doType(VK_F); break;
case 'g': doType(VK_G); break;
case 'h': doType(VK_H); break;
case 'i': doType(VK_I); break;
case 'j': doType(VK_J); break;
case 'k': doType(VK_K); break;
case 'l': doType(VK_L); break;
case 'm': doType(VK_M); break;
case 'n': doType(VK_N); break;
case 'o': doType(VK_O); break;
case 'p': doType(VK_P); break;
case 'q': doType(VK_Q); break;
case 'r': doType(VK_R); break;
case 's': doType(VK_S); break;
case 't': doType(VK_T); break;
case 'u': doType(VK_U); break;
case 'v': doType(VK_V); break;
case 'w': doType(VK_W); break;
case 'x': doType(VK_X); break;
case 'y': doType(VK_Y); break;
case 'z': doType(VK_Z); break;
case 'A': doType(VK_SHIFT, VK_A); break;
case 'B': doType(VK_SHIFT, VK_B); break;
case 'C': doType(VK_SHIFT, VK_C); break;
case 'D': doType(VK_SHIFT, VK_D); break;
case 'E': doType(VK_SHIFT, VK_E); break;
case 'F': doType(VK_SHIFT, VK_F); break;
case 'G': doType(VK_SHIFT, VK_G); break;
case 'H': doType(VK_SHIFT, VK_H); break;
case 'I': doType(VK_SHIFT, VK_I); break;
case 'J': doType(VK_SHIFT, VK_J); break;
case 'K': doType(VK_SHIFT, VK_K); break;
case 'L': doType(VK_SHIFT, VK_L); break;
case 'M': doType(VK_SHIFT, VK_M); break;
case 'N': doType(VK_SHIFT, VK_N); break;
case 'O': doType(VK_SHIFT, VK_O); break;
case 'P': doType(VK_SHIFT, VK_P); break;
case 'Q': doType(VK_SHIFT, VK_Q); break;
case 'R': doType(VK_SHIFT, VK_R); break;
case 'S': doType(VK_SHIFT, VK_S); break;
case 'T': doType(VK_SHIFT, VK_T); break;
case 'U': doType(VK_SHIFT, VK_U); break;
case 'V': doType(VK_SHIFT, VK_V); break;
case 'W': doType(VK_SHIFT, VK_W); break;
case 'X': doType(VK_SHIFT, VK_X); break;
case 'Y': doType(VK_SHIFT, VK_Y); break;
case 'Z': doType(VK_SHIFT, VK_Z); break;
case '`': doType(VK_BACK_QUOTE); break;
case '0': doType(VK_0); break;
case '1': doType(VK_1); break;
case '2': doType(VK_2); break;
case '3': doType(VK_3); break;
case '4': doType(VK_4); break;
case '5': doType(VK_5); break;
case '6': doType(VK_6); break;
case '7': doType(VK_7); break;
case '8': doType(VK_8); break;
case '9': doType(VK_9); break;
case '-': doType(VK_MINUS); break;
case '=': doType(VK_EQUALS); break;
case '~': doType(VK_SHIFT, VK_BACK_QUOTE); break;
case '!': doType(VK_EXCLAMATION_MARK); break;
case '@': doType(VK_AT); break;
case '#': doType(VK_NUMBER_SIGN); break;
case '$': doType(VK_DOLLAR); break;
case '%': doType(VK_SHIFT, VK_5); break;
case '^': doType(VK_CIRCUMFLEX); break;
case '&': doType(VK_AMPERSAND); break;
case '*': doType(VK_ASTERISK); break;
case '(': doType(VK_LEFT_PARENTHESIS); break;
case ')': doType(VK_RIGHT_PARENTHESIS); break;
case '_': robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_MINUS); robot.keyRelease(KeyEvent.VK_SHIFT); break;
case '+': doType(VK_PLUS); break;
case '\t': doType(VK_TAB); break;
case '\n': doType(VK_ENTER); break;
case '[': doType(VK_OPEN_BRACKET); break;
case ']': doType(VK_CLOSE_BRACKET); break;
case '\\': doType(VK_BACK_SLASH); break;
case '{': doType(VK_SHIFT, VK_OPEN_BRACKET); break;
case '}': doType(VK_SHIFT, VK_CLOSE_BRACKET); break;
case '|': doType(VK_SHIFT, VK_BACK_SLASH); break;
case ';': doType(VK_SEMICOLON); break;
case ':': robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SHIFT); break;
case '\'': doType(VK_QUOTE); break;
case '"': doType(VK_QUOTEDBL); break;
case ',': doType(VK_COMMA); break;
case '<': doType(VK_SHIFT, VK_COMMA); break;
case '.': doType(VK_PERIOD); break;
case '>': doType(VK_SHIFT, VK_PERIOD); break;
case '/': doType(VK_SLASH); break;
case '?': doType(VK_SHIFT, VK_SLASH); break;
case ' ': doType(VK_SPACE); break;
default:
throw new IllegalArgumentException("Cannot type character " + character);
}
}
private void doType(int... keyCodes) {
doType(keyCodes, 0, keyCodes.length);
}
private void doType(int[] keyCodes, int offset, int length) {
if (length == 0) {
return;
}
robot.keyPress(keyCodes[offset]);
doType(keyCodes, offset + 1, length - 1);
robot.keyRelease(keyCodes[offset]);
}
}
答案 2 :(得分:-1)
Selenium提供了使用send_keys()方法上传文件的选项。我不知道你使用的语言是什么,所以我将在Python中提供一个例子。
在屏幕截图中我可以看到这是你的输入元素:
<input type="file" firmware-upgrade="firmware"/>
假设您已找到此元素并保存在元素变量中。 如果我们想要将文件上传到此,我们需要做的就是使用send_keys()方法并将文件路径作为参数。
element = function_to_locate_element()
element.send_keys('path\\to\\file')
这样就不需要使用机器人了。