Reputation: 57
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
Upvotes: 3
Views: 7030
Reputation: 2672
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:
Upvotes: 0
Reputation: 38424
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();
Upvotes: 1