How to find ExtJS elements with dynamic id

后端 未结 2 1086
暖寄归人
暖寄归人 2020-12-20 01:42

I am working with Selenium and need to get the ID of a control but the ID is dynamic and I can\'t get it.


相关标签:
2条回答
  • 2020-12-20 02:01

    Simple with CSS selectors.

    Your selector would be,

    em[id^='button-'][id$='-btnWrap']
    

    Or if you wanted to click the button in there -

    em[id^='button-'][id$='-btnWrap'] > button
    
    0 讨论(0)
  • 2020-12-20 02:14

    I have been answering questions like for many times, you might want to have a look.

    ExtJS pages are hard to test, especially on finding elements.

    Here are some of the tips I consider useful:

    • Don't ever use dynamically generated IDs. like (:id, 'button-1122-btnEl')
    • Don't ever use absolute/meaningless XPath, like //div[4]/div[3]/div[4]/div/div/div/em/button

    • Take advantage of meaningful auto-generated partial ids and class names. (So you need show more HTML in your example, as I can make suggestions.)

      For example, this ExtJS grid example: (:css, '.x-grid-view .x-grid-table') would be handy. If there are multiple of grids, try index them or locate the identifiable ancestor, like (:css, '#something-meaningful .x-grid-view .x-grid-table').

    • Take advantage of button's text.

      For example, this ExtJS example: you can use XPath .//span[contains(@class, 'x-btn-inner') and text()='Send']. However, this method is not suitable for CSS Selector or multi-language applications.

    • The best way to test is to create meaningful class names in the source code. If you don't have the access to the source code, please talk to your manager, using Selenium against ExtJS application should really be a developer's job. ExtJS provides cls and tdCls for custom class names, so you can add cls:'testing-btn-foo' in your source code, and Selenium can get it by (:css, '.x-panel .testing-btn-foo').

    Other answers I made on this topic:

    • How to find unique selectors for elements on pages with ExtJS for use with Selenium?
    • How to click on elements in ExtJS using Selenium?
    • Using class names in Watir
    • how to click on checkboxes on a pop-up window which doesn't have name, label

    Note: sircapsalot's answer doesn't work for ExtJS, because most of buttons have CSS Selector em[id^='button-'][id$='-btnWrap'], (but can't blame him, as this is quite ExtJS specific, some people might not be familiar with).

    0 讨论(0)
提交回复
热议问题