获取文本并将该单词替换为所选文本

问题描述 投票:0回答:2

我想从css选择器的文本中替换一个单词

company_name = browser.find_element_by_css_selector("body > company.b1").text

让它像这样的文字“GBH Global”

description = browser.find_element_by_css_selector("body > p.b1").text

让我们用这样的文字“这是我们在伦敦的公司名称”

我希望这样做,并将公司名称替换​​为GBH Global,例如“这是GBH Global,我们位于伦敦”

company_description  = browser.find_element_by_css_selector("body > p.b1 > input") 
company_description.send_keys(description) 

我想发送像这样的“”这是GBH全球我们在伦敦“使用selenium和python

我有这个文本“这是我们在伦敦的公司名称”,我可以更改其代码格式以正常工作...

python selenium selenium-webdriver replace str-replace
2个回答
0
投票

大概是由代码行提取的文本:

browser.find_element_by_css_selector("body > company.b1").text

即GBH Global将是一个变量。在这种情况下,您可以替换文本companyname,如下所示:

company_name  = browser.find_element_by_css_selector("body > company.b1").text
description  = browser.find_element_by_css_selector("body > p.b1").text
text_to_replace = browser.find_element_by_css_selector("body > company.b1").text.split()[2]
print(description.replace(text_to_replace, "{}".format(company_name)))

0
投票

以下应该工作

company_name  = "GBH Global"
description  = "This is companyname we are based in london"
company_description = description.replace("companyname", company_name)
© www.soinside.com 2019 - 2024. All rights reserved.