Pressing Ctrl+A in Selenium WebDriver

前端 未结 13 869
一向
一向 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:19

    One more solution (in Java, because you didn't tell us your language - but it works the same way in all languages with Keys class):

    String selectAll = Keys.chord(Keys.CONTROL, "a");
    driver.findElement(By.whatever("anything")).sendKeys(selectAll);
    

    You can use this to select the whole text in an <input>, or on the whole page (just find the html element and send this to it).


    EDIT - after OP stated that he's using Selenium Ruby bindings

    There's no chord() method in the Keys class in Ruby bindings. Therefore, as suggested by Hari Reddy, you'll have to use Selenium Advanced user interactions API, see ActionBuilder:

    driver.action.key_down(:control)
                 .send_keys("a")
                 .key_up(:control)
                 .perform
    
    0 讨论(0)
  • 2020-11-27 03:22
    Actions act = new Actions(driver);
    act.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).build().perform();
    
    0 讨论(0)
  • 2020-11-27 03:25
    WebDriver driver = new FirefoxDriver();
    
    Actions action = new Actions(driver); 
    
    action.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();
    

    This method removes the extra call ( String.ValueOf() ) to convert unicode to string.

    0 讨论(0)
  • 2020-11-27 03:27

    You could try this:

    driver.findElement(By.xpath(id("anything")).sendKeys(Keys.CONTROL + "a");
    
    0 讨论(0)
  • 2020-11-27 03:29

    For Python:

    ActionChains(driver).key_down(Keys.CONTROL).send_keys("a").key_up(Keys.CONTROL).perform();
    
    0 讨论(0)
  • 2020-11-27 03:29

    I found that in ruby, you can pass two arguments to send_keys

    Like this:

    element.send_keys(:control, 'A')

    0 讨论(0)
提交回复
热议问题