3

How can I do two action in selenium WebDriver 2 at the same time ? I need to press and keep CTRL and click on link. I would like to see some solution in C#.

This is not working.

Actions builder = new Actions(_driver);
builder.SendKeys(Keys.Control).Click(link).KeyUp(Keys.Control);
IAction multiple = builder.Build();
multiple.Perform();

Thank very much for answers

3
  • when I use KeyDown it is not working too Commented Mar 7, 2013 at 14:23
  • can you do Ctrl + Enter Key ? (Enter to simulate click) Commented Mar 7, 2013 at 15:20
  • I think Ctrl + Space is for multiselect Commented Oct 7, 2014 at 7:55

2 Answers 2

1

You're ignoring the return value of your builder. Try:

Actions builder = new Actions(_driver);
builder = builder.KeyDown(Keys.Control).Click(link).KeyUp(Keys.Control);
IAction multiple = builder.Build();
multiple.Perform();

or even an equivalent shorthand of this:

new Actions(_driver)
    .KeyDown(Keys.Control)
    .Click(link)
    .KeyUp(Keys.Control)
    .Perform();
Sign up to request clarification or add additional context in comments.

Comments

0

If you can't get the Actions to work, you can bail out and invoke javascript (or jQuery, as in my example here), to invoke the Ctrl-Click.

Example html fragment (that you're trying to automate the testing of)...

<script type='text/javascript'>
    function myClick(e) {if(e.ctrlKey) {alert('ctrl+click');}}
</script>

...

<img id='myElement' onclick='myClick();' src='abc.gif' />

Example c# call:

public void ExecuteJs(string javascript)
{
    var js = Browser.WebDriver as IJavaScriptExecutor;
    if (js != null) js.ExecuteScript(javascript);
}

public void CtrlClickElement(string elementId)
{
    var script = string.Format("var e=jQuery.Event('click');e.ctrlKey=true;$('#{0}').trigger(e);", elementId);
    ExecuteJs(script);
}

...

CtrlClickElement("myElement");

Reference:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.