How to modify innerHTML in selenium python, i tried the code, but it didnt worked, can anyone, please help me out.
xpath_desc = '//*[@id="cke_1_contents"]/div/p'
element = driver.find_element(By.XPATH, xpath_desc)
HTML = 'Hello World This is <a href="http://google.com">Google</a>'
driver.execute_script("arguments[0].setAttribute('innerHTML', {HTML}", element)
I have tried changing the innerHTML
of a <p>
tag and I am able to do it using the below code.
The HTML code which I have used to recreate the scenario -
<html>
<head>
<title>ForTesting</title>
</head>
<body>
<p id="myP">Change the Paragraph Here</p>
</body>
</html>
Code -
# Change the InnerHTML of a P Tag.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
import time
driver = webdriver.Chrome()
action = ActionChains(driver)
driver.get("URL")
time.sleep(5)
Para_Element = driver.find_element_by_id("myP")
print(Para_Element.text)
# driver.execute_script("var ele=arguments[0]; ele.innerHTML = 'Successfully Changed the Content';", Para_Element)
driver.execute_script("document.getElementById('myP').appendChild(document.createElement('a'))")
element = driver.find_element_by_tag_name("a")
driver.execute_script("arguments[0].setAttribute('href', 'https://www.google.com/')", element)
driver.execute_script("var ele=arguments[0]; ele.innerHTML = 'Google';", element)
Para_Element = driver.find_element_by_id("myP")
print(Para_Element.text)
Let me know if you have any questions about that. Please take the reference from the above code and then try.