【问题标题】:Selenium python find and replace textSelenium python 查找和替换文本
【发布时间】:2022-07-13 11:47:12
【问题描述】:

我正在尝试使用 Selenium 和 Python 替换 Wagtail 后端字符串中的文本。 我的策略是:

  • 使用 contains(text() 查找包含文本的元素
  • 替换我需要替换的字符串(在这种情况下是一些价格)
  • 用新文本替换旧文本

element = driver.find_element(By.XPATH,"//*[contains(text(), '17,30')]")
newprice= str(element.text).replace('17,30',"1000")
driver.execute_script("arguments[0].innerText =  ",newprice, element)
这是我收到的错误消息

selenium.common.exceptions.JavascriptException:消息:javascript 错误:意外标识符

以下代码有效,但不能满足我的需要。包含“17,30”的元素还包含其他文本。使用以下代码,整个文本将替换为“1000”。

element = driver.find_element(By.XPATH,"//*[contains(text(), '17,30')]")
driver.execute_script("arguments[0].innerText =  '1000'", element)

【问题讨论】:

    标签: javascript python selenium


    【解决方案1】:

    你可以这样做:

    driver.execute_script(f"arguments[0].innerText = {newprice}", element)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-30
      • 2013-05-27
      • 2011-08-20
      • 2011-11-24
      • 2021-10-25
      • 2020-08-23
      • 2012-08-14
      相关资源
      最近更新 更多