Modify innerHTML using Selenium

a 夏天 提交于 2019-12-17 18:59:04

问题


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?


回答1:


Try this:

WebElement element = ...
((JavascriptExecutor)driver).executeScript(
  "var ele=arguments[0]; ele.innerHTML = 'my new content';", element);



回答2:


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.



来源:https://stackoverflow.com/questions/35922259/modify-innerhtml-using-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!