Ctrl + Click to select multiple items

I have a multiselect box that I need to test. Is there a way I can ctrl + click to select multiple values from this box?

I don’t think that you can perform this using the built-in keywords, you will need to use Selenium:

List<WebElement> options = WebUiCommonHelper.findWebElements(findTestObject('path/to/my/object'), 30);
Actions actions = new Actions(DriverFactory.getWebDriver());
actions.keyDown(Keys.LEFT_CONTROL)
    .click(options.get(0))
    .click(options.get(1))
    .click(options.get(2))
    .keyUp(Keys.LEFT_CONTROL)
    .build()
    .perform();

Your Test Object will need to identify all of the option elements, or you will need to find them another way (using Selenium’s ‘Select’ class, for example).

1 Like

I hava similar problem and thought to use your solution, but sadly I’m not god at selenium and web search :wink:

I have a table and want to select 2 elements and then click on a button.

My List:

I tried to use your mentioned code example:

List options = WebUiCommonHelper.findWebElements(findTestObject(‘Object Repository/ListView/Record_List’), 30);
Actions actions = new Actions(DriverFactory.getWebDriver());
actions.keyDown(Keys.LEFT_CONTROL)
.click(options.get(0))
.click(options.get(1))
.click
.keyUp(Keys.LEFT_CONTROL)
.build()
.perform();

The TO “Record_List” is defined as xpath (//tr[starts-with(@id,‘treeview-’) and contains(@id,‘-record-root’)])

I thought that I have to use this TO and all following -tags would be in options[0], options[1], …

But sadly it doesn’t worked.

But it failed with the error “java.lang.IndexOutOfBoundsException: Index: 1, Size: 1” → for me the List just includes one item

Next I tried to use the selenium select-class:

def driver = DriverFactory.getWebDriver()
//selenium = new WebDriverBackedSelenium(driver, baseUrl)

Select oSelect = new Select(driver.findElement(WebUiCommonHelper.findWebElement(By.xpath(“//tr[starts-with(@id,‘treeview-’) and contains(@id,‘-body’)]”), 5)))

oSelect.selectByVisibleText(“2020190789”);

// Using sleep command so that changes can be noticed
Thread.sleep(2000);

oSelect.selectByVisibleText(“2020190624”);
Thread.sleep(2000);

I got at “Select oSelect” the error “groovy.lang.MissingPropertyException: No such property: By for class: Script1551953808883” → looks like it can’t find the object, but the xpath is correct.

I found a third solution

WebDriver driver = DriverFactory.getWebDriver()

‘To locate table’
WebElement Table = driver.findElement(By.xpath(“//tr[starts-with(@id,‘treeview-’) and contains(@id,‘-body’)]”))
‘To locate rows of table it will Capture all the rows available in the table’
List rows_table = Table.findElements(By.tagName(‘td’))

Actions actions = new Actions(DriverFactory.getWebDriver());
actions.keyDown(Keys.LEFT_CONTROL)
.click(rows_table.get(0))
.click(rows_table.get(1))
.click
.keyUp(Keys.LEFT_CONTROL)
.build()
.perform();

At “WebElement Table” I got the error “groovy.lang.MissingPropertyException: No such property: By for class: Script1551953808883” → but xpath is correct.

Did I forgot something, have you a hint for me?

Thanks

You just need to import the By class:

import org.openqa.selenium.By

1 Like

Thanks for this hint. It helped me to identify that my xpath wasn’t correct :roll_eyes:

But after correction my code looks like:

WebDriver driver = DriverFactory.getWebDriver()

‘To locate table’
WebElement Table = driver.findElement(By.xpath("//tr[starts-with(@id,‘treeview-’) and contains(@id,’-record-root’)]"))
‘To locate rows of table it will Capture all the rows available in the table’
List rows_table = Table.findElements(By.tagName(‘td’))

Actions actions = new Actions(DriverFactory.getWebDriver());
actions.keyDown(Keys.LEFT_CONTROL)
.click(rows_table.get(0))
.click(rows_table.get(1))
.keyUp(Keys.LEFT_CONTROL)
.build()
.perform();

But I get this in the log after the .perform()-command:

1551982874208 Marionette WARN TimedPromise timed out after 0 ms: stacktrace:
bail@chrome://marionette/content/sync.js:196:64

Any idea?

Not sure, never seen that before, but it doesn’t look like an error, just a warning. Did your script do what it was supposed to?

No, it doesn’t.

There is no selection on any element.

Now I looked again on the example and changed my code as followed:

WebDriver driver = DriverFactory.getWebDriver()

‘To locate table’
WebElement Table = driver.findElement(By.xpath("//tbody[starts-with(@id,‘treeview-’) and contains(@id,’-body’)]"))
‘To locate rows of table it will Capture all the rows available in the table’
List rows_table = Table.findElements(By.xpath("//tr[starts-with(@id,‘treeview-’) and contains(@id,’-record-’)]"))

Actions actions = new Actions(DriverFactory.getWebDriver());
actions.keyDown(Keys.LEFT_CONTROL)
.click(rows_table.get(3))
.click(rows_table.get(4))
.keyUp(Keys.LEFT_CONTROL)
.build()
.perform();

And now it works.

I’m so grr, of course I have to set the tbody to the element “WebElement Table” :roll_eyes:

For someone who is interested at the source of the example:

https://docs.katalon.com/katalon-studio/tutorials/handle_web_tables.html#example-1you-want-to-get-a-text-from-a-web-table-and-verify-it

(At section “Example 1: You want to get a text from a Web table and verify it.”)

But your hint was a big step.

Thanks.

1 Like

To get this to work, I had to import all of the following:

import org.openqa.selenium.By as By
import org.openqa.selenium.WebDriver as WebDriver
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import org.openqa.selenium.interactions.Actions as Actions
import org.openqa.selenium.Keys as Keys

I appreciate that this is an old thread but it might be of use to others looking for a solution.

I’ve used the following built in keywords in order to select multiple values, with x, y, z being the index numbers of the options to be selected (starting from 0).

WebUI.selectOptionByIndex(findTestObject(‘Promotions/AddPromotion/Select_Products’), ‘x,y,z’)