3

The Title pretty much explains everything. I'm searching for a way to change let's say

<h1 id="title">Title<h1>

to

<h1 id="title">Nothing<h1>

I'm kind of desperate since

driver.FindElement(By.Id("title")).Text;

doesn't allow assignment.

1
  • What on earth are you trying to do? I'm very curious. You don't normally use Webdriver to change the elements on a page; it's for interacting with it the way a human would, and humans don't usually edit the HTML directly. May 6, 2015 at 16:44

2 Answers 2

7

Quickly glanced over the properties and methods of IWebElement, doesn't look like they can set values. However, you can easily execute some javascript snippet to change HTML in your page.

IWebDriver driver;
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("document.getElementById("title").innerHTML = "New text!";");

You might need to escape the inner quotation marks, haven't been able to run an actual test. Or use single apostrophes.

1
  • you should not even need to create a IJavaScriptExecutor you can just do driver.ExecuteScript()
    – Malachi
    Aug 4, 2016 at 13:59
0

I did this on Selenium Vba and it worked fine:

attrScript00 = "document.getElementById('title').innerText = 'New text2!'"
driver.ExecuteScript attrScript00

attrScript00 = "document.getElementById('title').innerHTML = 'New text3!'"
driver.ExecuteScript attrScript00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.