I need to interact with an online application written by a third-party developer, part of which is written in React. To interact with the site I'm driving Firefox via Selenium and Python. I can use traditional CSS selectors to interact with most of the site, but the parts written in React have random autogenerated class names that change every time the devs rebuild their app. There are no IDs found on any DOM elements except for the root node.
How can I get a unique CSS Selector for a DOM element, given a specific React Component in the virtual DOM?
For instance, by using React Develper Tools I see this tree:
Context.Provider
├─ Context.Provider
│ └─ gs
├─ Context.Provider
│ └─ styled.div
│ └─ styled.button
└─Iv
How can I get a unique CSS Selector for the DOM element that represents the Context.Provider > Context.Provider > styled.div > styled.button React Component?
I have seen the React Element Selector Query NodeJS library, but I need something that can run in Python, or can be interpreted from within Python and Selenium.
Here is example code from the app I'm targeting:
<div id="root">
<div class="sc-hTtwUo kyYkJm">
<div class="sc-jqUVSM bZjAcv">
<button class="sc-jSMfEi kXusAw">Foo</button>
<button class="sc-eCYdqJ ddMHci">Bar</button>
</div>
<div class="sc-kgUAyh bqLSQt">
<div class="sc-kgflAQ kGhjap">
<div class="sc-kDDrLX civFbY">
... Lots more ...
</div>
</div>
</div>
</div>
</div>
Other than the button element text, that is actual production HTML. Note that this is not deliberate obfuscation, rather this is not uncommon for React apps.