'How to save part of a URL as a variable using Robot Framework

I wonder if you would be able to help

I have a page where I perform a save action and the resulting page gives me an ID in the URL

e.g. https://thiscouldbeanything.co.uk/Timesheet/Detail/115325

What I would like to do is take the 6 digit id at the end of the URL and save it as a variable for use later on in the test. I know you can use Get Location the to store the whole URL but I wondered if there was a way (probably a python keyword option) to take just the ID.

I thought maybe about a python function that would take the Get Location as an argument and then a way to chop the URL up and pass the id as a variable back, but I was unsure how.

Any help would be greatly appreciated.

Many thanks



Solution 1:[1]

here some python code can help you get your ID in your URL

def getLastID(url):
    lastId=  url.rsplit('/', 1)[-1]
    return lastId

Solution 2:[2]

You can use robot framework keyword 'Split String' of your URL. Then, get last element of list, ${list}[-1]

Example:

${url}=  get location
@{splitted_url}=   Split String    ${url}   /
${id}=  evaluate   ${splitted_url}[-1]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sidara KEO
Solution 2 ???? ??????????