I have capture the current URL on the page.
using :
String url = driver.getCurrentUrl();
Now I want a specific text inside this string. Let say
String url = http://www.youtube.com/watch?v=R5-gtsdenpE
and I want
Capture and store the current page URL in String URL. (Done)
Capture the text in the URL after "v" and store it in String emb. (??)
I am using JAVA to write my scripts on Ubuntu.
Use:
String fullURL = http://www.youtube.com/watch?v=R5-gtsdenpE;
String emb = fullURL.split("\\?v=")[1];
This is what you want I guess-
String string = "http://www.youtube.com/watch?v=R5-gtsdenpE";
URL url = new URL(string);
System.out.println(url.getQuery());
Handle the exception appropriately.
In case you don't want to use URL class, just search for the first index of ? and then use substring() to get the string after that.
System.out.println(string.substring(string.indexOf("?")+1));
Url url = new Url(driver.getCurrentUrl());
Map<String, String[]> params = parameterMapFromString(url.getQuery());
String v = params.get("v")[0];
If you are requirement is static and you are sure that you have to get the value after "v" than you can try this also
String emb = url.substring(url.indexOf("v"), url.length()).trim();
Related
I have one request which response is displayed as url below. I need to extract user_token value from url and pass to subsequent request.
response url: http://example.com?user_token=0c1c59bc-3aaa-40f1-b978-7172de09a27f&m_id=9999&code=200&is_register=false&M=SUCCESS
i want to extract user_token from it and want to pass it to subsequent request, need solution in java code not java script.
You can do this:
String getTokenId(String url){
String[] splitUrl = url.split("user_token=");
String tokenId = "";
if(splitUrl.length >1){
for(int i =0; i < splitUrl[1].length(); i++){
if(splitUrl[1].charAt(i) == '&'){
tokenId = splitUrl[1].substring(0,i);
break;
}
}
}
return tokenId;
}
But if you know the exact length of the token, it could be a search.
Maybe what you need is request.getParameter("user_token"). You can do this in servlet for example
How can I get the value "2" of "entityId=2" from this example Url: https://test.com/form/form.htm?Index=0&entityId=2&wid=74&_wid=74 using Java and assuming this value is not static?
I am using:
URL url = new URL("https://test.com/form/form.htm?Index=0&entityId=2&wid=74&_wid=74");
String quesries = url.getQuery();
int i = quesries.length();`enter code here`
System.out.println(i);
Which gave me length of 33.
You just have to use the substring method in Java.
String entityId = urlString.substring(urlString.indexOf("entityId=")+9,urlString.indexOf("&wid"))
Let's say I have a page which lists things and has various filters for that list in a sidebar. As an example, consider this page on ebuyer.com, which looks like this:
Those filters on the left are controlled by query string parameters, and the link to remove one of those filters contains the URL of the current page but without that one query string parameter in it.
Is there a way in JSP of easily constructing that "remove" link? I.e., is there a quick way to reproduce the current URL, but with a single query string parameter removed, or do I have to manually rebuild the URL by reading the query string parameters, adding them to the base URL, and skipping the one that I want to leave out?
My current plan is to make something like the following method available as a custom EL function:
public String removeQueryStringParameter(
HttpServletRequest request,
String paramName,
String paramValue) throws UnsupportedEncodingException {
StringBuilder url = new StringBuilder(request.getRequestURI());
boolean first = true;
for (Map.Entry<String, String[]> param : request.getParameterMap().entrySet()) {
String key = param.getKey();
String encodedKey = URLEncoder.encode(key, "UTF-8");
for (String value : param.getValue()) {
if (key.equals(paramName) && value.equals(paramValue)) {
continue;
}
if (first) {
url.append('?');
first = false;
} else {
url.append('&');
}
url.append(encodedKey);
url.append('=');
url.append(URLEncoder.encode(value, "UTF-8"));
}
}
return url.toString();
}
But is there a better way?
The better way is to use UrlEncodedQueryString.
UrlEncodedQueryString can be used to set, append or remove parameters
from a query string:
URI uri = new URI("/forum/article.jsp?id=2¶=4");
UrlEncodedQueryString queryString = UrlEncodedQueryString.parse(uri);
queryString.set("id", 3);
queryString.remove("para");
System.out.println(queryString);
I have a string url like http://google.com. I need to remove 'http://' from the URL. I have tried the method .replace("http://",""), but it is not working.
Web web = org.getWeb();
webUrl = web.getUrl();
out.println("webUrl :"+webUrl ); // here it prints:: http://google.com
webUrl.replace("http://","");
out.println("webUrl :"+webUrl ); // here also it prints:: http://google.com
Try:
webUrl = webUrl.replace("http://","");
As replace returns the replaced string
You need to do like following.String is immutable class.replace will return you new String object.
webUrl = webUrl.replace("http://","");
Refer String is immutable. What exactly is the meaning?
I would like to parse a string which is basically a URL. I need to check simply that a parameters is passed to it or not.
so http://a.b.c/?param=1 would return true http://a.b.c/?no=1 would return false and http://a.b.c/?a=1&b=2.....¶m=2 would return true since param is set
I am guessing that it would involve some sort of regular expression.
Java has a builtin library for handling urls: Spec for URL here.
You can create a URL object from your string and extract the query part:
URL url = new URL(myString);
String query = url.getQuery();
Then make a map of the keys and values:
Map params<string, string> = new HashMap<string, string>();
String[] strParams = query.split("&");
for (String param : strParams)
{
String name = param.split("=")[0];
String value = param.split("=")[1];
params.put(name, value);
}
Then check the param you want with params.containsKey(key);
There is probably a library out there that does all this for you though, so have a look around first.
String url = "http://a.b.c/?a=1&b=2.....¶m=2";
String key = "param";
if(url.contains("?" + key + "=") || url.contains("&" + key + "="))
return true;
else
return false;