Identifying Elements by Sibling Attributes

4 posts, 0 answers
  1. Galacia
    Galacia avatar
    2 posts
    Member since:
    Feb 2017

    Posted 03 Mar 2017 Link to this post

    In the web app I'm testing, there is a button which appears multiple times on a page, attached to dynamic objects. There is no identifier for each unique instance. I want to click a specific instance of it, but the identifier for this button is in a sibling section of the HTML.

    Simplified model:

    <div class = "whole">
          <div class = "top">
                 <p title = "Identifier"></p>

          </div>
          <div class = "bottom">
                 <button>ButtonIWantToClick</b>

          </div>

    </div>

    Is there a way to use nested finds to determine which button to click, and if not, what might be the best way to tackle it in code?

    I've done some digging in the forums to see if there was a solution to this specific issue. I'm new to Telerik, so here's to hoping that there's a reasonably simple way of achieving what I want to do.

    Thanks in advance.

  2. Nikolay Petrov
    Admin
    Nikolay Petrov avatar
    308 posts

    Posted 10 Mar 2017 Link to this post

    Hi Galacia,

    The following lines of code will find and click on the button from the given html example:
    HtmlDiv myDiv = ActiveBrowser.Find.ByAttributes<HtmlDiv>("class=bottom");
    Element myButton = myDiv.ChildNodes[0];
    Actions.Click(myButton);

    I hope these directions are helpful.

    Regards,
    Nikolay Petrov
    Telerik by Progress
     
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
     
  3. Galacia
    Galacia avatar
    2 posts
    Member since:
    Feb 2017

    Posted 10 Mar 2017 in reply to Nikolay Petrov Link to this post

    Maybe I wasn't clear, but there are multiple instances of the 'top' and 'bottom' class on the page, a top and bottom for each 'whole'. But there is an identifier for the 'whole' object (of which there are multiple also), located in the 'top' section of each.
  4. Nikolay Petrov
    Admin
    Nikolay Petrov avatar
    308 posts

    Posted 15 Mar 2017 Link to this post

    Hi Galacia,

    I created sample code lines that will handle the html provided sample case.

    If the class="whole" is the unique attribute the code should be modified like this:
    HtmlDiv myDiv = ActiveBrowser.Find.ByAttributes<HtmlDiv>("class=whole");
    Element myButton = myDiv.ChildNodes[1].ChildNodes[0];
    Actions.Click(myButton);

    The idea here is to locate the most closest to the button element with the unique ID or attribute and to try to locate it among its child or sibling elements. The other approach is to use Xpath but it is not recommended in general since it is easy to brake if some parts in application changes.

    I hope this clarification helps.

    Regards,
    Nikolay Petrov
    Telerik by Progress
     
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
     
Back to Top