How to extract a date in a specific format from URL using Selenium?

天涯浪子 提交于 2020-12-12 05:10:18

问题


Using:

String URL = driver.getCurrentUrl();

I get something like this:

firstname=&lastname=&sex=Male&exp=1&datepicker=11%2F30%2F2020&photo=&continents=asia&submit=

I have to extract the date and to print it in this format: yyyy-mm-dd, that is, to get 2020-11-30. Does anyone have any idea? Thanks!


回答1:


I'm going to assume that getCurrentUrl() returns the full url not just the query string.

At a high level I see the process as

  1. Url decode the string
  2. Parse the query string into a map
  3. Transform the datepicker value from mm-dd-yyyy to yyyy-mm-dd which is just iso8601

Steps 1 and 2 can be done with a library such as OkHttp

final HttpUrl currentUrl = HttpUrl.parse(driver.getCurrentUrl());
if (currentUrl != null) {
    final String inputDate = currentUrl.queryParameter("datepicker");
}

If you don't want to use a/that library there are some zero dependency options in this other question.

Step 3 has some options depending how robust you need this.

  1. Direct string manipulation.
  2. Use a library or built in date utility.

If this doesn't matter much the string manipulation would be the simplest. Just create a new string with the yyyy, a '-', and the mm-dd.

The downside is that it doesn't offer any way to validate the date.

Here is a library option

SimpleDateFormat parseFormatter = new SimpleDateFormat("dd-MM-yyyy");
Date date = parseFormatter.parse(inputDate);

SimpleDateFormat outputFormatter = new SimpleDateFormat("yyyy-MM-dd");

String date = outputFormatter.format(date);
System.out.println(date);



回答2:


  1. You can create a Map<String, String> paramMap out of the given URL string.
  2. Process the URL string and store the parameter name as the key and the parameter-value as the value into this Map.
  3. Get the value of the key, datepicker from this Map and parse it to a date object and format the date object into a string of your desired format. Since the date-time API of java.util and their formatting API, SimpleDateFormat are outdated and error-prone, I suggest you do it using the modern date-time API. Learn more about the modern date-time API at Trail: Date Time.

I have written solution using both, the modern java.time date-time API as well as the legacy java.util date-time API.

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) throws UnsupportedEncodingException, ParseException {
        String strUrl = "https://www.google.com?firstname=&lastname=&sex=Male&exp=1&datepicker=11%2F30%2F2020&photo=&continents=asia";
        Map<String, String> map = getParams(strUrl);
        String strDate = map.get("datepicker");

        // Using Java-8
        System.out.println(LocalDate.parse(strDate, DateTimeFormatter.ofPattern("MM/dd/uuuu")));

        // Using legacy API
        DateFormat sdfInput = new SimpleDateFormat("MM/dd/yyyy");
        DateFormat sdfOutput = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(sdfOutput.format(sdfInput.parse(strDate)));
    }

    public static Map<String, String> getParams(String strUrl) throws UnsupportedEncodingException {
        Map<String, String> paramMap = new HashMap<String, String>();
        String[] parts = URLDecoder.decode(strUrl, StandardCharsets.UTF_8.name()).split("\\?");
        if (parts.length == 2) {
            String[] params = parts[1].split("&");
            for (String param : params) {
                String[] nameVal = param.split("=");
                if (nameVal.length == 2) {
                    String name = nameVal[0];
                    String value = nameVal[1];
                    paramMap.put(name, value);
                }
            }
        }
        return paramMap;
    }
}

Output:

2020-11-30
2020-11-30


来源:https://stackoverflow.com/questions/65060587/how-to-extract-a-date-in-a-specific-format-from-url-using-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!