简体   繁体   中英

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

eg 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

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

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

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM