Pressing Ctrl+A in Selenium WebDriver

前端 未结 13 876
一向
一向 2020-11-27 02:45

I need to press Ctrl+A keys using Selenium WebDriver. Is there any way to do it?

I checked the Selenium libraries and found that Selenium allow

相关标签:
13条回答
  • 2020-11-27 03:43

    By using Robot class in Java:

    import java.awt.Robot;
    import java.awt.event.KeyEvent;
    
    public class Test1 
    {
        public static void main(String[] args) throws Exception 
        {
            WebDriver d1 = new FirefoxDriver();
            d1.navigate().to("https://www.youtube.com/");
            Thread.sleep(3000);
            Robot rb = new Robot();
            rb.keyPress(KeyEvent.VK_TAB);
            rb.keyRelease(KeyEvent.VK_TAB);
            rb.keyPress(KeyEvent.VK_TAB);
            rb.keyRelease(KeyEvent.VK_TAB); 
    
            // Perform [Ctrl+A] Operation - it works
            rb.keyPress(KeyEvent.VK_CONTROL);
            rb.keyPress(KeyEvent.VK_A);
            //It needs to release key after pressing
            rb.keyRelease(KeyEvent.VK_A);
            rb.keyRelease(KeyEvent.VK_CONTROL);
            Thread.sleep(3000);
        }
    }
    
    0 讨论(0)
提交回复
热议问题