How to hold down the control key in watir webdriver

1.5k views Asked by At

Im stuck in my project today with a small issue , i need to hold the Control Key for a while and select the {n} number of rows.

Below is my code snippet any help is really appreciated .

!!!...I want hold the control key click on a row ,then click on the other row and finally release the control key ...!!!

while (enclosure_count_remove > 0)

     $browser.element(:xpath,"//table[@id ='selectConfiguration']/tbody/tr[0]").click(:control);
     $browser.element(:xpath,"//table[@id ='selectConfiguration']/tbody/tr[1]").click(:control);
     enclosure_count_remove -= 1;

end

And the second way i tried is given below

while (enclosure_count_remove > 0)

     $browser.send_keys :control
     $browser.element(:xpath,"//table[@id ='selectConfiguration']/tbody/tr[0]").click;
     $browser.element(:xpath,"//table[@id ='selectConfiguration']/tbody/tr[1]").click;
     enclosure_count_remove -= 1;

end

Both are not working for me, every time im able to click but failed to hold and click both the entities .

Thanks!

Aditya

2

There are 2 answers

4
Justin Ko On

After looking at the watir-webdriver Element.click() method, I think you have to use the webdriver action builder directly. Try this:

action = $browser.driver.action
action.key_down(:control)
action.click $browser.element(:xpath,"//table[@id ='selectConfiguration']/tbody/tr[0]").wd
action.click $browser.element(:xpath,"//table[@id ='selectConfiguration']/tbody/tr[1]").wd
action.key_up(:control)
action.perform

I do not have a webpage to test this so am going purely based on documentation.

3
Chuck van der Linden On

The click method takes modifier parameters. That should allow you to do clicks with any number of keys 'held down'. See the rdocs on the click method for details You should be able to do

browser.table(:id => 'selectConfiguration').row[0].click(:control)

Of course that presumes that you know for sure which rows you want to click and that they would never change. Normally I'd be selecting the rows based on some matching some unique bit of text or other content, more similar to how a human might pick the rows to select.

browser.table(:id => 'selectConfiguration').row(:text => /text_fragment/).click(:control)