2

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.

  • 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. – Yamikuronue May 6 '15 at 16:44
6

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.

  • you should not even need to create a IJavaScriptExecutor you can just do driver.ExecuteScript() – Malachi Aug 4 '16 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, privacy policy and cookie policy

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