2017-11-04 4 views
2

シフト+ ctrl + sをセレンに入力するには? あなたは、単にそれを押し下げ、その後、各キーコード初回ためwebdriverを、その後、与えられたキーを一連のキーを送信する場合、私はその投げエラーセレンにシフト+ ctrl + sを入力する方法

答えて

1

Actions action = new Actions(driver); 
action.sendKeys(Keys.chord(Keys.SHIFT + Keys.CONTROL + "s")).perform(); 

を使用していました。
だからあなたのコードsendKeys(Keys.chord(Keys.SHIFT + Keys.CONTROL + "s")が時間内に一連のイベント以下に相当します。

  1. Shiftキーを押しながら
  2. 踏込SHIFT
  3. プレス制御
  4. 押し下げのCONTROL
  5. sを押し
  6. 押し下げs

Sキーが押された瞬間にCtrlとShiftを押してを保持していると思われるので、これはあなたが望むものではありません。


あなたは鍵を解放するためにキーを押すと、押した状態でそれを残すためにActions#keyDownメソッドを使用して、後でActions#keyUpする必要があります。だから、アクションのシーケンスは次のようになります。

  1. Shiftキーを押しながら - keyDown
  2. 押してCtrlキーを使用して - keyDown
  3. を使用すると、キーを押し、次にS(このキーを押すと、すぐにsendKeys方法を使用して解放することができます)
  4. をリリースCtrl-Shift-Sを押すと表示されるまで待ちます。
  5. Release Shift - using keyUp

テストコードの後で予期しない影響(Ctrl + Shiftを押したままにしないでください)を避けるために、ポイント5と6(リリースキー)を実行する必要があります。


ここには、Webドライブコードをテストするのに役立つsimple page on jsfiddleへのリンクがあります。

<body> 

<p>Press a key on the keyboard in the input field to find out if the Ctrl-SHIFT key was pressed or not.</p> 

<input id="ctrl_shift_s" type="text" onkeydown="isKeyPressed(event)"> 

<p id="demo"></p> 

<script> 
function isKeyPressed(event) { 
    console.log(event.keyCode); 
    var x = document.getElementById("demo"); 
    if (event.shiftKey && event.ctrlKey && event.keyCode == 83) { 
     x.innerHTML = "The Ctrl-SHIFT-S keys were pressed!"; 
    } else { 
     x.innerHTML = "Please press Ctrl-SHIFT-S"; 
    } 
} 
</script> 

</body> 

あなたはその後、(この要素のid =「ctrl_shift_s」)このページの入力フィールドにカーソルを移動し、Ctrl + Shift + Sキー(SHIFTキーとCtrlを保持している)を押した場合メッセージが表示されますCtrl-Shift-Sキーが押されました!以下は enter image description here


最新のIEやFirefoxとChromeのドライバを使用して上記のテストページagaistテスト(作業)のコード例です。 IEドライバでActionsを実行するには、requireWindowFocus();オプションを使用する必要があります。

WebDriver driver= null; 
try{ 

    System.setProperty("webdriver.ie.driver", "C:\\instalki\\IEDriverServer.exe"); 
    System.setProperty("webdriver.chrome.driver", "C:\\instalki\\chromedriver.exe"); 
    System.setProperty("webdriver.gecko.driver", "C:\\instalki\\geckodriver.exe"); 

    InternetExplorerOptions opt = new InternetExplorerOptions(); 
       opt.requireWindowFocus(); 
    //   driver=new InternetExplorerDriver(opt); 
    //   driver = new ChromeDriver(); 
    driver = new FirefoxDriver(); 

    driver.manage().window().maximize(); 

    WebDriverWait wait = new WebDriverWait(driver, 10); 

    driver.get("https://jsfiddle.net/39850x27/2/"); 
    final By inputField = By.id("ctrl_shift_s"); 
    final By messageWeWaitFor = By.xpath("//*[text() = 'The Ctrl-SHIFT-S keys were pressed!' ]"); 
    final By frame = By.name("result"); 

    // Swift to a frame (our test page is within this frame) 
    driver.switchTo().frame(driver.findElement(frame)); 

    // move a corsor to the field 
    wait.until(ExpectedConditions.elementToBeClickable(inputField)).click(); 

    Actions a = new Actions(driver); 

    // Press SHIFT-CTRL-S    
    a.keyDown(Keys.SHIFT) 
    .keyDown(Keys.CONTROL) 
    .sendKeys("s") 
    .build() 
    .perform(); 


    //Wait for a message 
wait.until(ExpectedConditions.visibilityOfElementLocated(messageWeWaitFor)); 

    System.err.println("Success - Ctrl-Shift-S were pressed !!!"); 

    // Sleep some time (to see the message is really on the page)   
    Thread.sleep(5000); 

    // Release SHIFT+CTRL keys 
    a.keyUp(Keys.CONTROL) 
    .keyUp(Keys.SHIFT) 
    .build() 
    .perform(); 

}finally { 
    if(driver!=null) { 
     driver.quit(); 
    } 
} 
関連する問題