[英]Java Selenium Run through Command Prompt CMD Windows Without IDE
[英]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.