Pages

Search This Blog

Tuesday, May 18, 2010

How to identify dynamic element in selenium

Many web sites create dynamic element on their web pages where Ids of the elements gets generated dynamically. Each time id gets generated differently. So to handle this situation we use some JavaScript functions.

starts-with

if your dynamic element's ids have the format <button id="continue-12345" /> where 12345 is a dynamic number you could use the following

XPath: //button[starts-with(@id, 'continue-')]  

contains

Sometimes an element gets identfied by a value that could be surrounded by other text, then contains function can be used.
To demonstrate, the element <input class="top suggest business"> can be located based on the ‘suggest’ class without having to couple it with the ‘top’ and ‘business’ classes using the following

XPath: //input[contains(@class, 'suggest')].

29 comments:

  1. Actual problem is ,
    When i click Select tag button,xpath value changes like:
    1st time i get---->id('fpey_selectTag')
    2nd time i get---->id('dmht_selectTag')
    3rd time i got------>id('gajx_selectTag')
    so on...

    I wrote my test case as

    selenium.type("gajx_tags", "a,b,c,");
    and test failed.Please tell me what should i include in place of
    gajx_tags in above line.
    Thanks in advance

    ReplyDelete
  2. Hi Farhin,
    Go through the below blogs you will get info about that .

    http://automationtricks.blogspot.com/2010/05/how-to-identify-dynamic-element-in.html

    ReplyDelete
  3. Can you please send me your html of your elements ?

    ReplyDelete
  4. Hi Niraj,

    I have the situation like having multiple radio buttons with the dynamic values like radio_{x} where x is the dynamic values.The value x came from DB.

    I used the following selenium command to select the radio button.
    selenium.click(starts-with(@id, 'radio-'));

    But the problem is every time it selects the first radio button only but i need to select second and third ...etc.

    Thanks
    Santhosh.

    ReplyDelete
  5. Then you read this post in this condition.

    http://automationtricks.blogspot.com/2010/09/how-to-locate-element-which-have-same.html

    ReplyDelete
  6. F-202222|-2@locationName


    I want ti Replace 2 with regular expression.

    ReplyDelete
  7. Problem:DropDown List:Selenium-perl
    In my application contains one dropdown box,i am unable to identify the dorpdownlist elements based on id.because it's changing every time dynamically.

    For ex:1st time:id=uki-list-1234-row-1
    2 nd time:id=uki-list-4123-row-1

    I have tried in another way to identify the id:

    like this..
    target://div[starts-with(@id:'uki-list-')]
    value :label=regexp:\uki-list-\d+-\row-\d?
    but i got the error Locator not found.

    ReplyDelete
  8. Problem:DropDown List:Selenium-perl

    Actually "select" command is used to select the dropdown box. "selectAndWait: for selct the options in drop down box based on label reg.expressons or id's,but in my application select command is not working.there is any other commands for selecting dropdown elements. please tell me.
    My dropdown box is devloped by uki java script frame work

    ReplyDelete
  9. I was very much impressed with your blog, Thanks a lot for the information.

    ReplyDelete
  10. Hi i have a situation where in i need to select an item from a drop down list and click on go button which would populate next drop down list.i used select,mouseDown nothing works.my element identifier not locating the element properly

    ReplyDelete
  11. hello my case is something different here is a button in my case and its value changed every time for example first value was "CommonBucket_'ct102-'_btnHold" and second time value was "CommonBucket_'ct104-'_btnHold"... how can i solve this problem please suggest me a best solution.

    Thanks in Advance

    ReplyDelete
  12. Aw, this was an exceptionally nice post. Taking the time
    and actual effort to make a superb article… but what can I say… I put things off a whole lot and don't manage to get anything done.

    My site; smile

    ReplyDelete
  13. Τhank you so much for this. I'm a blogger coming from Niederelbert, Germany and what I just read here on blogger.com couldn't bе wгіttеn muсh better.
    Рerusing thiѕ poѕt rеminds mе оf my old гoοmie,
    Faustino. He pегsiѕtently κept ρreаching abоut this.
    I most cеrtainly will send thіѕ
    stuff to him. Pretty sure hе wіll hаve a
    gooԁ time reаding this. I аm thankful to уou you fοr
    ѕhаring thiѕ.

    Alsо visit my webpаge :: outdoor pizza oven

    ReplyDelete
  14. in xpath if input id =1234 & 1234 is dynamic, how to locate an element in selenium?

    ReplyDelete
  15. this is exactly what we want

    ReplyDelete
  16. HI
    I have one drop down list element. which dynamically changes every time."//div[9]/div[2]/div[3]/div[3]/select" where "div[3]/select" is constant.
    so can i identify that ? do we have ends-with function as like starts with function.

    ReplyDelete
  17. there is some function called as contains similar to start with . you can use it

    ReplyDelete
  18. This comment has been removed by the author.

    ReplyDelete
  19. Hi,

    my $test = $driver->find_element("q" , "name");

    by using "$test" i can pass inputs to textbox, but is there any way to get "ID" of textbox using "$test".

    Please help
    Thank you

    ReplyDelete
  20. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium.
    Selenium

    ReplyDelete
  21. It was very nice blog to learn about Selenium.Thanks for sharing new things.selenium training in chennai

    ReplyDelete
  22. Thanks for sharing information about Selenium training, I read your entire post and realize that your post is really informative. Such type of post is really helpful for those people who want to learn Selenium training. Apart from this I read another informative post for same kind of information. For your further information I am sharing that informative post about Selenium training. http://www.ducatindia.com/seleniumtraining/

    ReplyDelete
  23. Hi Niraj
    I have one doubt, i have one text box whose id keeps chaging below is the HTML Code
    input id="contact_type0" class="form-control ng-dirty ng-valid-parse ng-valid ng-valid-required ng-touched" name="contact_type" ng-model="data.contact_type" placeholder="Contact Type" required="" aria-invalid="false" style="" type="text"/
    2nd time if i click Add New Button
    it gives me
    input id="contact_type1" class="form-control ng-dirty ng-valid-parse ng-valid ng-valid-required ng-touched" name="contact_type" ng-model="data.contact_type" placeholder="Contact Type" required="" aria-invalid="false" style="" type="text"
    So i have written the xpath="//input[starts-with(@id,'contact_status')]" but works well with first time but 2nd time it fails it always identify the 1st text box, could you please tell how to write the generic xpath which will work and i am looping this code for 1st works well , in the 2nd unable to identify the 2nd text box instead it identify the first one. please help me out.

    ReplyDelete
    Replies
    1. I need help on same issue.Please let me know any one have soln

      Delete