Home > Enterprise >  Selenium > Replace the last part of an URL
Selenium > Replace the last part of an URL

Time:04-20

I have a URL with some dynamic parts

https://{env}-my-website.{domain}/UnDefault.aspx

And every time I want to go to another module I have to replace this part

UnDefault.aspx

with the module to which I want to navigate e.g.

Modules/Repairs/SNH.aspx

Is there an easy way to do that with Selenium or Regex?

this is what i tried :

String currentUrl = driver.getCurrentUrl();
String[] results = StringUtils.substringsBetween(currentUrl, "https", "?:cn|com");
String SNH= results[0] "Modules/Repairs/SNH.aspx";
driver.navigate().to(SNH);

CodePudding user response:

To replace the path part of the url (everything after the server):

String url = url.replaceAll("^(.*?//[^/] /).*", "$1Modules/Repairs/SNH.aspx");
  • Related