chinmay raskar
chinmay raskar

Reputation: 11

How to identify an element whose id is dynamically changing and has no other attributes in selenium?

I've this html body for a kendo dropdown list which has only one attribute i.e. id which is dynamically changing, how do i identify this object on every page refresh accurately. other attributes like class and tab index already are present with same values multiple times on the same page for other dropdowns-

<span role="listbox" unselectable="on" class="k-dropdown-wrap k-state-default" id="dde13a91-2bf3-4e41-af72-bee1b881a8d9" dir="ltr" readonly="false" tabindex="0" aria-disabled="false" aria-readonly="false" aria-haspopup="true" aria-expanded="false" aria-owns="48f666d8-4c3c-43a8-a4dc-8e7a9961a0ef" aria-activedescendant="ca3c4431-3ebf-46c0-9510-a64a32eae108-C.US.0000110896">
            <span unselectable="on" class="k-input">
               <!---->
               <!---->2018 ALBERTSONS / Beverage Mixes
           </span>
           <span unselectable="on" class="k-select">
               <span class="k-i-arrow-s k-icon"></span>
           </span>
           <!---->
        </span>

Upvotes: 0

Views: 1150

Answers (3)

chinmay raskar
chinmay raskar

Reputation: 11

I found a workaround to locate the element:

First get the xpath of the span tag inside the dropdown using it's text as below: //text()[contains(.,'2018 ')] since '2018 ' is common irrespective of adjacent text

Then move up to the parent tag of the dropdown to locate the dropdown frame which can be collapsed on click: //text()[contains(.,'2018 ')]/parent::/parent::

then simply click on the element which is located. driver.findElement(By.xpath("//text()[contains(.,'2018 ')]/parent::/parent::")).click();

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193338

To click on the element with the only attribute-text as 2018 ALBERTSONS / Beverage Mixes you need to induce WebDriverWait for the element to be clickable and you can use the following solution:

  • (Java) xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='k-dropdown-wrap k-state-default' and @role='listbox']//span[@class='k-input']"))).click();
    

Upvotes: 1

Deryl
Deryl

Reputation: 66

If there is a always present value in dropdown, You can try to find this value by text or something, and then get a parent: XPath: Get parent node from child node

Other way is to get it by xpath of structure, like: div[3]/.../span etc. It is not good as every change can fail Your test, but if You have no other options, then You might want to try this.

Upvotes: 1

Related Questions