'How to open new URL in the same tab with Selenium, but with some way with editing the URL?

I want to open a new URL in the same tab in Selenium, but I need to somehow edit the URL. What working options are there? I am thinking about navigating by keys, but the problem is that the URL can't be inspected.



Solution 1:[1]

To open and edit the url on the same tab:

1) First you need to get the current page url, to do so:

getCurrentUrl command :

Usage : Used to get the URL of the currently opened web page in the browser.

Syntax : driver.getCurrentUrl();

Return type : String (Returns the URL of current web page)

Example :

String currentURL  = driver.getCurrentUrl();
System.out.println(currentURL);

2) Now editing url: With the above url, now you can change whatever to want to: example:

String currentURL  = driver.getCurrentUrl();
//Let assume CurrentUrl = {something}/product/id1/
String editURL = currentURL.replace("id1", "id2")
                     OR
String editURL = currentURL + "/something/";

3) Opening edited url on the same tab.

driver.get(editURL);
       OR
driver.navigate().to(editURL);

Solution 2:[2]

There is a Selenium method called getCurrentUrl. Use it like so,

String currentURL = driver.getCurrentUrl();
String newURL = currentURL + "yourEdit";

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abhishek Dhoundiyal
Solution 2 natn2323