2015-10-13 35 views
1

我有一些URL,如「https://some.url.ru/new-token」。我需要將其更改爲「http://some.url.ru/new-token」(意味着將HTTPS更改爲HTTP)。我無法更改整個網址,因爲每次我獲得新的令牌時。 (Selenium,Java)。我如何更改url鏈接並將其與硒一起使用

所以,我的問題:

  1. 我得到的網址:driver.getCurrentUrl();

  2. 然後我需要改變它 - 將HTTPS更改爲HTTP:如何?

  3. 我需要使用新的URL:如何?

我找到了解決辦法:

StringBuffer newUrl = new StringBuffer(driver.getCurrentUrl()); 
newUrl.deleteCharAt(4); //4 - it's number of symbol what should cut, first symbol have number "0" 
driver.get(String.valueOf(newUrl)); //there url would be without 5th symbol. 

所以:NOW質疑仍停留的兩個一: 「一些」 到 「另一個」 例如,在如何變化?

+0

是y我們的問題與https安全使用網址,但你想要得到它使用http?你的問題嚴重違反了網絡安全!請通過適當的研究來更新問題。 [檢查如何在SO問](http://www.stackoverflow.com/help/how-to-ask) –

+0

@GirishSortur我的問題與安全無關。有時候我需要改變部分,比如「some」到「another」(例如「https://some.url.ru/new-token」)。 – Lzhelis

+0

是否要取URL,並將其改爲http然後'.get(url)'。或者更改'href'併發送點擊操作? –

回答

0

您可以使用.replaceAll()改變串,如:

String url = driver.getCurrentUrl(); 
String url_new = url.replaceAll("string/to/change", "new/string"); 
driver.navigate().to(new_url); 
0

java.lang.String類提供了很多方法來對字符串工作。通過這些方法的幫助,我們可以對字符串執行操作,例如修剪,連接,轉換,比較,替換字符串等等。在你的情況下,例如用'http'替換'https',你可以做這樣的事情:

String url = driver.getCurrentUrl(); 
String newUrl = url.replace("https", "http"); 

不知道有什麼用情況,但如果你想瀏覽到這個新的URL,然後做

driver.get(newUrl); 

同樣,要改變「一些」到「另一個」你問:

String url = driver.getCurrentUrl(); 
String newUrl = url.replace("some", "another"); 
+0

如果「一些」是動態的,那麼「some」可能是「smthelse」,對我而言重要的是打開「另一個」的url? – Lzhelis

相關問題