繁体   English   中英

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

[英]Unable to write in Windows Run prompt through Selenium

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

    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);
          }

        }
}
}

这段代码尝试打开该目录,并且可以正常运行,我已经对其进行了测试。

花了一些时间才能找出问题所在。 您没有释放密钥,这就是为什么它对您不起作用的原因,并且您还必须处理可能出现的不同密钥代码。 该机械手类仅写入AZ。

这是一些可能会派上用场的关键事件的列表-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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM