简体   繁体   English

如何使用Java在Selenium WebDriver中按“ALT + S”

[英]How to press “ALT+S” in Selenium WebDriver using Java

I need to Send ALT+S Key event using Selenium Web Driver for an ``EditBox. 我需要使用Selenium Web Driver为``EditBox发送ALT+S Key事件。 Cursor Position is already set to EditBox I am using following code 光标位置已设置为EditBox我正在使用以下代码

driver.switchTo().activeElement().sendKeys(Keys.chord(Keys.ALT+"S"))

but it's not giving me desired result. 但它没有给我想要的结果。 It's Typing character 'S' in the Edit Box. 它是在编辑框中键入字符'S' I have tried another code but got the same result. 我尝试了另一个代码,但得到了相同的结果。

Actions action =new Actions(driver);
action.keyDown(Keys.ALT).sendKeys(String.valueOf('\u0053')).perform();

Thanks in Advance 提前致谢

I want to Add one more thing here. 我想在这里再补充一点。 The code is working Properly in Firefox 12 but its not working properly in IE9 代码在Firefox 12中正常运行,但在IE9中无法正常运行

Cross-browser issues are rather hard to investigate as they are specific to particular driver and not WebDriver API. 跨浏览器问题很难调查,因为它们特定于特定驱动程序而不是WebDriver API。

Another variant that might work. 另一种可能有效的变体。

driver.findElement(By.xpath("your editbox's XPath")).sendKeys(Keys.chord(Keys.ALT, "s"));

As workaround I might recommend to take a look to AutoIT ( Official site ) or Robot ( Java Doc ) 作为解决方法,我可能会建议您查看AutoIT( 官方网站 )或Robot( Java Doc

Try this. 尝试这个。 It might work, I haven't tried though 它可能会奏效,但我没试过

driver.findElement(By.xpath("your editbox's XPath"))
      .sendKeys(Keys.chord(Keys.ALT + Keys.S));

You can achieve this by using Robot class of java 你可以通过使用Robot的java类来实现这一点

    try{
        Robot robot=new Robot();
        robot.keyPress(KeyEvent.VK_ALT);
        Thread.sleep(1000);
        robot.keyPress(KeyEvent.VK_S);
        robot.keyRelease(KeyEvent.VK_ALT);
        robot.keyRelease(KeyEvent.VK_S);        
    }
    catch(Exception ex){
        System.out.println(ex.getMessage());
    }

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

相关问题 与Selenium一起使用时,使用Robot Class的ALT + S键无法正常工作 - ALT+S key press using Robot Class not working when used along with Selenium 如何使用Java在Selenium WebDriver中按“TAB”然后按“ENTER”键? - How to press “TAB” then “ENTER” key in Selenium WebDriver using Java? 使用 java 在 Selenium WebDriver 中按下(Ctrl + 鼠标单击) - Key press in (Ctrl + mouse click) in Selenium WebDriver using java 在 Eclipse 中使用 Selenium WebDriver 和 java 代码在搜索字段中输入文本后如何按“Enter” - How to press 'Enter' once text is entered in the search field using Selenium WebDriver and java code in eclipse 如何使用 Java 在 selenium WebDriver 中按 CTRL+T 和 CTRL+TAB? - How to press CTRL+T and CTRL+TAB in selenium WebDriver using Java? 如何通过 Selenium WebDriver 使用 Java 按 Ctrl+A 到 select 页面中的所有内容 - How to press Ctrl+A to select all content in a page by Selenium WebDriver using Java 如何使用Selenium WebDriver按Ctrl + 0(零) - How to press Ctrl+0 (Zero) using Selenium WebDriver 在 Java 中使用 Selenium WebDriver - Using Selenium WebDriver with Java 使用Java的Selenium Webdriver - Selenium Webdriver using java 如何使用java实现与Selenium WebDriver的PhantomJS - How to implement PhantomJS with Selenium WebDriver using java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM