gpt4 book ai didi

java - 无法通过 Selenium 在 Windows 运行提示符下写入

转载 作者:行者123 更新时间:2023-11-30 07:44:04 25 4
gpt4 key购买 nike

我需要通过 selenium Webdriver 访问一个共享路径,但无法通过 Selenium 在 Windows 运行提示符中写入该共享路径 "\\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

关于java - 无法通过 Selenium 在 Windows 运行提示符下写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34174875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com