Key press in (Ctrl+A) Selenium WebDriver

◇◆丶佛笑我妖孽 提交于 2019-12-17 03:57:33

问题


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 allows key press of special and functional keys only.


回答1:


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



回答2:


To click Ctrl+A, you can do it with Actions

  Actions action = new Actions(); 
  action.keyDown(Keys.CONTROL).sendKeys(String.valueOf('\u0061')).perform();

\u0061 represents the character 'a'

\u0041 represents the character 'A'

To press other characters refer the unicode character table - http://unicode.org/charts/PDF/U0000.pdf




回答3:


In Selenium for C#, sending Keys.Control simply toggles the Control key's state: if it's up, then it becomes down; if it's down, then it becomes up. So to simulate pressing Control+A, send Keys.Control twice, once before sending "a" and then after.

For example, if we is an input IWebElement, the following statement will select all of its contents:

we.SendKeys(Keys.Control + "a" + Keys.Control);




回答4:


You could try this:

driver.findElement(By.xpath(id("anything")).sendKeys(Keys.CONTROL + "a");



回答5:


Since Ctrl+A maps to ASCII code value 1 (Ctrl+B to 2, up to, Ctrl+Z to 26).

Try:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Internal;
using OpenQA.Selenium.Remote;

namespace SeleniumHqTest
{
    class Test
    {
            IWebDriver driver = new InternetExplorerDriver();
            driver.Navigate().GoToUrl("http://localhost");
            IWebElement el = driver.FindElement(By.Id("an_element_id"));
            char c = '\u0001'; // ASCII code 1 for Ctrl-A
            el.SendKeys(Convert.ToString(c));
            driver.Quit();
    }
}



回答6:


For Python:

ActionChains(driver).key_down(Keys.CONTROL).send_keys("a").key_up(Keys.CONTROL).perform();



回答7:


Simplest answer in C# (if you are C# inclined).

Actions action = new Actions(); 
action.KeyDown(OpenQA.Selenium.Keys.Control).SendKeys("a").KeyUp(OpenQA.Selenium.Keys.Control).perform();

This answer is almost given above by Hari Reddy but I have fixed the case which he'd got wrong on some keywords, added the KeyUp or you get in a mess leaving the control key down, I've also added the clarification on OpenQA.Selenium.Keys because you may also be using Windows.Forms on the same class as I was an require this clarity. Lastly, I type "a" because I found that to be the simplest way and I can see no suggestion from the OP that they don't want the simplest answer.

Many thanks to Hari Reddy though as I was a novice in Actions class usage and I was writing many different commands, chaining them together the way he showed is quicker :-)




回答8:


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.




回答9:


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

Like this:

element.send_keys(:control, 'A')




回答10:


It works for me:

OpenQA.Selenium.Interactions.Actions action 
    = new OpenQA.Selenium.Interactions.Actions(browser);
action.KeyDown(OpenQA.Selenium.Keys.Control)
    .SendKeys("a").KeyUp(OpenQA.Selenium.Keys.Control).Perform();



回答11:


This is what worked for me using C# (VS2015) with Selenium:

new Actions(driver).SendKeys(Keys.Control+"A").Perform();

You can add as many keys as wanted using (+) inbetween.




回答12:


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);

        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);
        Thread.sleep(3000);
    }
}



回答13:


Actions act = new Actions(driver);
act.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).build().perform();


来源:https://stackoverflow.com/questions/11503736/key-press-in-ctrla-selenium-webdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!