替换python中的一部分url

时间:2016-04-19 08:00:25

标签: python selenium

我需要在selenium webdriver + python中替换以下url的一部分:

https://ve-215:8443/cloudweb/dropbox_authorized?oauth_token=l8eYuFG8nux3TUHm&uid=69768040

我需要将ve-215替换为IP地址192.168.24.53

我尝试使用replace,但它不起作用。

以下是我正在使用的代码:

current_url=driver.current_url
print(current_url) #prints the url of the current window.

current_url.replace("ve-215", "192.168.53.116")
print(current_url)  #print url with replaced string
driver.get(current_url) #open window with replaced url

任何人都可以帮我解决上述代码的错误吗?

2 个答案:

答案 0 :(得分:2)

replace方法不会修改字符串本身(字符串在Python中是不可变的)但返回一个新字符串。尝试

current_url = current_url.replace("ve-215", "192.168.53.116")

话虽如此,建议使用{3}}模块(Python 3中的urlparse)来解析和重建URL。

答案 1 :(得分:1)

current_url = driver.current_url print(current_url) #prints the url of the current window. current_url = current_url.replace("ve-215", "192.168.53.116") print(current_url) #print url with replaced string driver.get(current_url) #open window with replaced url 方法返回应用了修改但未修改当前字符串的字符串。

你应该这样使用它:

src