jagdpanzer
jagdpanzer

Reputation: 713

Is it possible to inject html into my selenium tests?

So lets say we have WebElement foo. foo has a crazy looking, consistently changing xpath. Hardcoding foo gets really annoying when you have to constantly do so.

I just discovered the html edit on Firebug and it made me wonder, is there anyway I could just write in id values for use in my Selenium tests? The distinct lack of no id values in any of the WebElements under test is getting real old.

Thanks in advance.

Upvotes: 1

Views: 170

Answers (2)

StrikerVillain
StrikerVillain

Reputation: 3776

For the question, Is it possible to inject html into my selenium tests, the answer is yes. You can use JavaScriptExecutor to inject html into your WebPage.

((JavascriptExecutor) driver).executeScript("arguments[0].id = 'abc';", element);

Just as JeffC mentioned, for injecting the id or any HTML, you would have to identify the required element first. Also, the changes will be lost after you refresh the page.

Upvotes: 0

JeffC
JeffC

Reputation: 25610

Editing the HTML in Firebug or Chrome devtools is just transient. As soon as you close the browser, your changes go away. There is no real solution to what you are asking other than to request that your devs add IDs to the desired elements. I'm assuming you don't own the site or that's not possible or you would have mentioned it. Welcome to test automation where the site creators don't take test automation into account when creating a site... :)

Upvotes: 3

Related Questions