12

I have this element:

WebElement element = ...
string val = element.getAttribute("innerHTML");

All I want to do is to change this innerHTML on my web page.
Is it possible?

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
david hol
  • 1,272
  • 7
  • 22
  • 44
  • Welcome to Stack Overflow! In order to get a good reception here, you should show what you have tried. Have you tried some things to change the innerHtml? Have you searched the web for ways of doing this, and if so, what have you found? Please [edit] your question and tell us what you've found and what you've tried. It can make the difference between getting useful answers and getting downvoted and closed. Good luck! – S.L. Barth is on codidact.com Mar 10 '16 at 18:32

3 Answers3

25

Try this:

WebElement element = ...
((JavascriptExecutor)driver).executeScript(
  "var ele=arguments[0]; ele.innerHTML = 'my new content';", element);
Florent B.
  • 41,537
  • 7
  • 86
  • 101
7

Selenium WebDriver does not have any direct methods to do so to change the DOM itself. However we can use JavascriptExecutor to use javascript to modify the DOM.

check this example to change the background color. You will get an idea to change the innerHTML as well.

Community
  • 1
  • 1
vins
  • 15,030
  • 3
  • 36
  • 47
5

in python use this :

element = driver.find_element_by_id("some_id")
driver.execute_script("arguments[0].innerText = 'what_you_want_to_show'", element)
mamal
  • 1,791
  • 20
  • 14