How to check the box of any values passed as input parameters

Hello Katalians members,

hope you doing great. I have a page as shown in the screenshot. Without using object repository (using descriptive programming) how can i check a checkbox based on input parameters? I want to achieve this using a custom keyword?

For example, I put Wash as input parameter and the custom keyword checked the corresponding checkbox.

I count on you for your help.

1 Like

Maybe the below might work:

import org.openqa.selenium.By as By
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.WebElement as WebElement
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory

WebDriver driver = DriverFactory.getWebDriver()

@Keyword
public void clickProduct(String product) {
     WebElement ele = driver.findElement(By.xpath("//i[@role='presentation']/following-sibling::div[last()]/div[text()='${product}']"));
     if (ele != null) {
         if (ele.isDisplayed()) {
              WebElement checkbox = driver.findElement(By.xpath("//i[@role='presentation']/following-sibling::div//input"));
              checkbox.click();
         } 
    }   
}

Hello @grylion54 , it is not working

Huh? Help > Error Log

Note that, the checkbox is a pop up and should i replace “following-sibling” in the xpath?

The code I gave is for you to try and see if it can work. The xpath commands, “following-sibling” and “preceding-sibling”, allows you to move from one element to another at the same level. If you are familiar with writing pathways, you can use anything that gets you to the objects you want. I tried to start at the <i> tag, but you can try to start closer. You have the code. Change it so you can start closer to your object if you want.

Personally, I believe you need to insert some “wait” statements into your code to allow your browser to catch up to your code. So, the below statements may be your friend:

// use this when move to a new page
WebUI.waitForPageLoad(10)

// use this after move to a new page and whenever needed
WebUI.waitForElementVisible(findTestObject(...), 10)

// and ...
WebUI.waitForElementClickable(findTestObject(...), 10)

When you “record” there is no spatial aspect taken between your “clicks” and “drags”. In other words, KS doesn’t care how long it waits before you “click”, however, in “replay”, it is all about waiting until the objects are present and visible to accept your “clicks” etc. So, if you have an “element not interactable”, then you may be too fast and the object is not ready. Slow your code down with some “waits”.

Edit: if you do not use the Object Repository, there are Selenium commands that you can use that allow you to “wait”. Use them.

I have made an amendment to my script above so that it includes the text that you want as a starting point:

import org.openqa.selenium.By as By
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.WebElement as WebElement
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory

WebDriver driver = DriverFactory.getWebDriver()

@Keyword
public void clickProduct(String product) {
     WebElement ele = driver.findElement(By.xpath("//i[@role='presentation']/following-sibling::div[last()]/div[text()='${product}']"));
     if (ele != null) {
         if (ele.isDisplayed()) {
              WebElement checkbox = driver.findElement(By.xpath("//div[text()='${product}'])/parent::div/preceding-sibling::div//input");
              checkbox.click();
         } 
    }   
}

Hello,

there is an error in the code, see below:

Line 880

public void clickProduct(String product) {
	WebDriver driver = DriverFactory.getWebDriver()
	WebElement ele = driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Product Selection'])[1]/following::*[name()='svg'][1]"))
	if (ele != null) {
		if (ele.isDisplayed()) {
			WebElement checkbox = driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='${product}'])[1]/following::*[name()='svg'][1]"))
			checkbox.click()
		}
	}
}

The above code works for me

Since you have “hard coded” the input value in your first line as per the quote above, this would not be like you asked in the Post question, however, if you want to try, then just go with:

public void clickProduct(String product) {
	WebDriver driver = DriverFactory.getWebDriver()
	WebElement checkbox = driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='${product}'])[1]/following::*[name()='svg'][1]"))
	checkbox.click()
}

Hello @grylion54 , yes, i really wanted to make it work like in the post initially, but the code did not work :frowning: Do you have the correct codes?