Combine xpath to a string/Integer

Can anyone please look into the below issue. Here I am trying to Catenate Xpath with a random number. So that I can randomly select any item from the list(for example from 1 to 10). But the scripts produces a strange output.

*** Variables ***
${accounts} xpath://form/span[2]/span/span[2]/span/span/ul/li

*** Test Cases ***
Combine Xpath
${random_index} Evaluate random.randint(1, 10)
${element} Set Variable ${accounts}[${random_index}]
${element_concatenate} Catenate ${accounts}[${random_index}]
Log To Console ${element}
Log To Console ${element_concatenate}

Actual Output
: p # Output
: # Output

Expected Output
xpath://form/span[2]/span/span[2]/span/span/ul/li[3]

Hi KP,

This line looks ok to me

${element} Set Variable ${accounts}[${random_index}]

this is an except from one of my keywords that click a random link

	${count}= 	Get Element Count 	${xpath}
	${random}= 	Evaluate 	random.randint(1, ${count})
	# Scroll Element Into View 	(//a)[${random}]
	# Click Link    (//a)[${random}]
	${text}= 	Get Text 	(${xpath})[${random}]
	${href}= 	Get Element Attribute 	(${xpath})[${random}] 	href

In your case it would generate an xpath of (//form/span[2]/span/span[2]/span/span/ul/li)[3]

have you checked your generated xpath in devtools to make sure it gets you the element you wanted? sometimes you need to wrap the xpath in round brackets like I did to get a list of matches and then take the nth one

bot otherwise what you’re doing looks ok to me

Hope that helps

Dave.

2 Likes

It worked. Thank you for quick suggestion.

1 Like