Is it possible to generate an URL object from a path?
Use Url:fromRoute () and pass in the route's name and arguments. You can also use fromUri() for external paths. https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Url.php/function/Url%3A%3AfromRoute/8.2.x

I usually find the route name by searching the routing.yml files for the path.

Alternately, you can do this. How to get the Route object from an URI?