简体   繁体   中英

Robot Framework: Get a specific part of the url

I'm very new working with Python and Robot Framework, I have the following url:

https://site-of-the-company/r/x/a0l3h000001HlRxAAK/view

I'd like to get the id of the page: a0l3h000001HlRxAAK

Already tried using regex like this:

${url}=  Get Location
${id}=  Get Regexp Matches  ${url}  \\d

But got this:

${id} = ['0', '3', '0', '0', '0', '0', '0', '1']

If you are sure the structure of the page will remain the same, you can useurllib.parse.urlparse to get the path, split the path using str.split and access the id from the resulting array

I would suggest a generic solution using split from string library that is available in builtin ( robotframework ):

@{tokenized_uri}=   Split String  ${uri}   /
${domain} =     Get From List   ${tokenized_uri}    2
${_param} =     Get From List   ${tokenized_uri}    5

As you can see, the split happens with "/" char, then you get the element in the list you are interested in. There are other solutions, I found this one quite generic.

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