'Selenium python find and replace text

I'm trying to replace text within a string on Wagtail backend using Selenium and Python. My strategy is:

  • Find the element that contains the text with contains(text()
  • Replace the string I need to replace (in this case some price)
  • substitute the old text with the new one

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)
This is the error message I get

selenium.common.exceptions.JavascriptException: Message: javascript error: Unexpected identifier

The following code works but doesn't do what I need. The element that contains "17,30" also contains other text. With the following code, the whole text will be substituted by "1000".

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


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source