So I have a class that wraps a WebElement and adds functionality to make it more useful in my page objects. I now need to set the text of a text input, and the only way I can see to do this is to inject javascript to find the element and set its value attribute.
For simple cases where I have an id to go off, this is fairly simple, however where there are dynamically generated forms there might not be a useful id to go off - some of these are found by finding the enclosing form and then looking for a css selector within it, where there are other inputs matching the same css selector within other forms.
I can see that I could solve this by finding everything by xpath, however there must be a less brittle/disruptive solution. I know that you can get a WebElement from a Javascript executor, what I'm wondering is if it's possible to inject a javascript object reference to a given WebElement, and if not, a way to get a unique xpath at runtime from an existing WebElement.
I'm using python, but solutions in other languages would probably be transferrable.
Edit
I know that there is a webdriver element cache somewhere, and that I can get an element's id in that cache:
Is there any way of using this in the injected javascript?