0

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)
1
  • 1
    Please clarify that "it didnt worked" Commented Aug 6, 2020 at 7:50

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Swaroop, This one works fine, but what if i want to enter html content in it, can you help me on it.i want to put it a variable and call it on Para_Element = driver.find_element_by_id("myP") HTML = 'Hello World This is <a href="google.com">Google</a>' driver.execute_script("var ele=arguments[0]; ele.innerHTML = '{HTML}';", Para_Element)
Sure i will give it a try and post the updated code.
@sunny9495 I have edited my code, please give it a try and let me know the outcome.
Hi Swaroop, Thanks alot for the help, but iam inserting not only the given html code mate,iam using different html code to perform actions on it, iam expecting like this bro. content = 'Hello World This is <a href='google.com'>Google</a>, This is <a href="yahoo.com">Yahoo</a> driver.execute_script("var ele=arguments[0]; ele.innerHTML = '{content}';", Para_Element) I want to insert what ever the html code given on content variable on page mate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.