Need Help with XPath

Hello,

Hope you guys are doing fine, I am having one question about what should be my strategy here.

I want to randomly select size here. For e.g Out of avaialble S , M , L , XL . I want to randomly select one size.

Right now, I can select first available size using below xpath.

//div[@class='swatch clearfix size']//input[@type='radio' and not(@disabled)]

@manpreet.mukkar You may be able to use something like:

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


WebDriver driver = DriverFactory.getWebDriver()
List<WebElement> myList= driver.findElements(By.xpath('//div[contains(@class,"swatch-element size-element")]//input[@type="radio" and not(@disabled)]'))
int element_count = myList.size();
println ("Found count of " + element_count.toString());

(Edit: I amended the path to add your input radio buttons not being disabled to get the correct number of enabled buttons.)
element_count then can be used in your Random generation:

myElementCnt = Math.abs(new Random().nextInt() % element_count)

Finally, you might find the myElementCnt element you could make your xpath into an array, as below, or with the myList list of elements that you got your count from above:

(//div[contains(@class,"swatch-element size-element")]//input[@type="radio" and not(@disabled)])[myElmentCnt]

1 Like

@grylion54 I will try your suggestion. Thanks a lot for your help on this one.