替换python中url的一部分

替换python中url的一部分,python,selenium,Python,Selenium,我需要在selenium webdriver+python中替换以下url的一部分: 我需要将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") p

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

我需要将
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

有人能帮我解决上面代码的问题吗?

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

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

也就是说,建议使用模块(在Python 3中)来解析和重建URL。

方法
replace
返回一个应用了修改的字符串,但不修改当前字符串

您应该这样使用它:

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