This is a migrated thread and some comments may be shown as answers.

Identifying Elements by Sibling Attributes

3 Answers 172 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Galacia
Top achievements
Rank 1
Galacia asked on 03 Mar 2017, 04:20 PM

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.

3 Answers, 1 is accepted

Sort by
0
Nikolay Petrov
Telerik team
answered on 10 Mar 2017, 05:25 PM
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
 
0
Galacia
Top achievements
Rank 1
answered on 10 Mar 2017, 06:54 PM
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.
0
Nikolay Petrov
Telerik team
answered on 15 Mar 2017, 02:02 PM
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
 
Tags
General Discussions
Asked by
Galacia
Top achievements
Rank 1
Answers by
Nikolay Petrov
Telerik team
Galacia
Top achievements
Rank 1
Share this question
or