如何在硒中按shift + ctrl + s [英] How to press shift + ctrl + s in Selenium
问题描述
如何按Selenium中的shift + Ctrl + s键? 我使用了下面的代码:
How to press shift + ctrl + s in Selenium ? I have used the code below:
Actions action = new Actions(driver);
action.sendKeys(Keys.chord(Keys.SHIFT + Keys.CONTROL + "s")).perform();
其投掷错误
推荐答案
如果只发送一系列键,则每个键代码的Webdriver首先按一个给定键,然后将其按下.
因此,您的代码sendKeys(Keys.chord(Keys.SHIFT + Keys.CONTROL + "s")
相当于时间上的以下一系列事件:
If you simply send a series of keys, then Webdriver for each keycode first press a given key, then depress it.
So your code sendKeys(Keys.chord(Keys.SHIFT + Keys.CONTROL + "s")
is equivalent to the below series of events in the time:
- 按Shift
- 按下Shift
- 按CONTROL
- 按下CONTROL
- 按s
- 按下s
这不是您想要的,因为您希望在按下S键时按下Ctrl和Shift键并按住 .
This is not what you want, because you are excpecting that Ctrl and Shift have been pressed and are held at the moment of time when the S key is pressed.
您需要使用 Actions#keyUp 来释放密钥.因此,操作顺序可能是:
You need to use Actions#keyDown method to press the key and leave it in the pressed state, and later Actions#keyUp to release the key. So the sequence of actions might be:
- 按SHIFT键-使用
keyDown
- 按Ctrl键-使用
keyDown
- 按下然后释放S(可以使用
sendKeys
方法按下并立即释放此键) - 等待按Ctrl-Shift-S的可见效果
- 释放Ctrl-使用
keyUp
- 发布班次-使用
keyUp
- Press SHIFT - using
keyDown
- Press Ctrl - using
keyDown
- Press then release S (this key can be pressed and immediately released using
sendKeys
method) - Wait for an visible effect of pressing Ctrl-Shift-S
- Release Ctrl - using
keyUp
- Release Shift - using
keyUp
必须完成第5点和第6点(释放键)的操作,以避免以后在测试代码中产生意想不到的效果(不要将Ctrl + Shift保持为按下状态).
Points 5 and 6 (releasing keys) must be done in order to avoid unexpected effects later in the test code (don't leave Ctrl+Shift in a pressed state).
这里是 jsfiddle上的简单页面的链接,可帮助我们测试WebDriver代码.
Here is a link to simple page on jsfiddle which help us to test our WebDriver code.
<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>
如果将光标移动到此页面上的INPUT字段(此元素的id ="ctrl_shift_s"),然后按Ctrl-SHIFT-S键(按住SHIFT和Ctrl),则会出现一条消息,按下Ctrl-Shift-S键!
以下是使用最新的IE,Firefox和Chrome驱动程序对上述测试页进行了测试的示例(有效)代码.您必须使用requireWindowFocus();
选项才能在IE驱动程序中运行Actions
.
Below is an example (working) code tested agaist the above test page using latest IE,Firefox and Chrome drivers. You must use requireWindowFocus();
option in order to run Actions
in IE driver.
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();
}
}
这篇关于如何在硒中按shift + ctrl + s的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!