无法通过Selenium在Windows运行提示中进行写入



我需要通过selenium Webdriver访问一个共享路径,但我无法通过selenium在Windows Run提示符中写入该共享路径"\18.187.980.12\Logs\abc.log"。我正在使用机器人类打开运行提示

    Robot robot = new Robot();  // Robot class throws AWT Exception  
    Thread.sleep(2000); 
    robot.keyPress(KeyEvent.VK_WINDOWS);    
    Thread.sleep(2000);
    robot.keyPress(KeyEvent.VK_R);  
    Thread.sleep(2000);

这段代码正在打开运行提示,但我无法在运行提示中写入共享路径"\18.187.980.12\Logs\abc.log"。请建议下一步行动。我的新代码如下:

  package ForNewFramework;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
class RobotClass {
    Robot robot = new Robot(); // Robot class throws AWT Exception
    public static void main(String[] args) throws AWTException,
            InterruptedException {
        // WebDriver driver = new FirefoxDriver();
        new RobotClass();
    }
    public RobotClass() throws AWTException {
        robot.keyPress(KeyEvent.VK_WINDOWS); // press arrow down key of keyboard
                                                // to navigate and select Save
                                                // radio button
        robot.keyPress(KeyEvent.VK_R); // press arrow down key of keyboard to
                                        // navigate and select Save radio button
        type("Prateek");
    }
    private void type(String s) {
        byte[] bytes = s.getBytes();
        for (byte b : bytes) {
            int code = b;
            // keycode only handles [A-Z] (which is ASCII decimal [65-90])
            if (code > 96 && code < 123)
                code = code - 32;
            robot.delay(40);
            robot.keyPress(code);
            robot.keyRelease(code);
        }
    }
}

在一天结束时,所有字符串都是按键的结果,因此您可以通过传递正确的ASCII值(按键代码)在键盘上按键。

请找到您需要的全部代码:

package ForNewFramework;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
class RobotClass {
    Robot robot = new Robot(); // Robot class throws AWT Exception
    public static void main(String[] args) throws AWTException, InterruptedException {
        // WebDriver driver = new FirefoxDriver();
        new RobotClass().runWindows("\\18.187.980.12\\Logs\\abc.log");
    }

    public RobotClass() throws AWTException, InterruptedException {
    }
    public void runWindows(String run) throws InterruptedException{
        robot.keyPress(KeyEvent.VK_WINDOWS); // press arrow down key of keyboard
        robot.keyPress(KeyEvent.VK_R); // press arrow down key of keyboard to
        robot.keyRelease(KeyEvent.VK_R);
        robot.keyRelease(KeyEvent.VK_WINDOWS);
        type(run);
        robot.keyPress(KeyEvent.VK_ENTER);
    }
    private void type(String s) throws InterruptedException  {
        Thread.sleep(2000); 
        byte[] bytes = s.getBytes();
        for (byte b : bytes)
        {
          int code = b;
          // keycode only handles [A-Z] (which is ASCII decimal [65-90])
          if (code >=65 && code<=90){
              System.out.println(code);
              robot.delay(1000);
              robot.keyPress(KeyEvent.VK_SHIFT );
              robot.keyPress(code);
              robot.keyRelease(code);
              robot.keyRelease(KeyEvent.VK_SHIFT);

          }else if (code > 96 && code < 123) {
              code = code - 32;
              System.out.println(code);
              robot.delay(2000);
              robot.keyPress(code);
              robot.keyRelease(code);
          }
          else{
              robot.delay(2000);
              robot.keyPress(code);
              robot.keyRelease(code);
          }
        }
}
}

这段代码试图打开那个目录,并且运行得很好,我已经测试过了

花了一些时间才弄清楚出了什么问题。你没有释放钥匙,这就是为什么它对你不起作用,而且你必须处理可能出现的不同钥匙代码。这个机器人类只写A-Z。

以下是一些可能派上用场的关键事件列表-http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html#VK_R

您可以使用硒类的sendkey库存

Actions builder = new Actions(driver);
builder.keyDown(Keys.TAB).perform()

如果你想使用机器人类,它应该是这样的

driver.findElement(By.xpath("//label[text()='User Name:']/following::div/input")).sendKeys("UserName");
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_WINDOWS);
robot.keyRelease(KeyEvent.VK_R)

最新更新