Search code examples
phpseleniumselenium-webdriverxpathcodeception

How do I access a DOM element with a dynamic ID in Codeception?


I'm new to autotests and I'm just starting to master Codeception. I am writing an autotest and at the end of the autotest I need to delete the . But when creating a new theme, a delete button with a dynamic ID is generated for it. How do I access this button for each new topic? Here is part of the code! The button I need is called "Delete"

<span data-theme_id="12445247" class="control_item icon-box_hover empty_link with_icon_link js-deleteTheme">
                                                    <span class="icon-delete-summary mod__hover"></span>Delete</span>

Solution

  • As you can see yourself, the data-theme_id attribute of this element is dynamically generated for each browsing session. What is seems to be a fixed values are icon-delete-summary class name of first span element and the Delete text of the second span.
    So, I would guess the following XPath should work here:

    "//span[text()='Delete']"
    

    Or in case the former locator is not unique maybe this:

    "//span[contains(@class,'js-deleteTheme')]//span[contains(.,'Delete')]"