简体   繁体   English

从URL中提取随机生成的ID

[英]Extract a randomly generated ID from URL

I need to extract a randomly generated part of an URL for a Selenium Test in Java. 我需要为Java中的Selenium Test提取随机生成的URL部分。

When the browser opens a page, eg: 当浏览器打开页面时,例如:

/edit_person.html?id=eb58cea3a3772ff656987792eb0a8c0f

then I'm able to show the url with: 然后我就能用以下内容显示网址:

String url = driver.getCurrentUrl();

but now I need to get only the randomly generated ID after the equals sign. 但现在我需要在等号后只获得随机生成的ID。

How do I extract the value of parameter id once I have the entire URL as a string in variable url? 在将整个URL作为变量url中的字符串后,如何提取参数id的值?

URL.getQuery() will give the query portion as a String it is a simple regular expression match to isolate the part you want. URL.getQuery()将查询部分作为String,它是一个简单的正则表达式匹配,以隔离您想要的部分。

id=(.*) will get you what you want as long as it is the only thing in the query string. 只要它是查询字符串中唯一的东西, id=(.*)就能得到你想要的东西。

This is how managed to solve the problem: 这就是设法解决问题的方法:

String url = driver.getCurrentUrl();
        URL aURL = new URL(url);
        url = aURL.getQuery();
        String[] id = url.split("=");
        System.out.println(id[1]);  

Thanks to Jarrod Roberson! 感谢Jarrod Roberson!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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