1

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.

jagdpanzer
  • 693
  • 2
  • 10
  • 35
  • 1
    @alecxe That's really not a duplicate of this question. Yes, it's about changing attributes but OP is asking how to get around having to hardcode long/complicated XPaths. Changing attributes isn't going to solve this problem because you'd have to use the same XPath to find the element to change it's attribute... so that doesn't help. But.. the answer is there is no answer other than have dev insert IDs, etc. but I'm assuming that isn't an option here. – JeffC Nov 10 '15 at 01:05
  • @JeffC I'm afraid you are right. I was a bit unsure about closing it, but decided to proceed and wait for the OP to come back to me if I was wrong closing it. Thanks! – alecxe Nov 10 '15 at 01:07

2 Answers2

3

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... :)

JeffC
  • 22,180
  • 5
  • 32
  • 55
0

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.

StrikerVillain
  • 3,719
  • 2
  • 24
  • 41