I am using Java and Webdriver with TestNG. I have a text box where a user enters a medication name and then clicks a Go button. The program then runs a query to RxNorm and returns the Strength and Form of the medication name. The webpage has a div with an id of whatDosage in that div is a list of buttons with the appropriate strength/form for that medication.
My question is, how do I get either the number of elements in that div or a list of the strength/forms displayed to the user? Ultimately, I will be making a call to RxNorm with the same medication name and comparing the results.
I have tried the following:
(1) String resultsWebPage = driver.findElements(By.id("whatDosage")).toString();
System.out.println(resultsWebPage);
(2) int resultsWebPage = driver.findElements(By.id("whatDosage")).size();
System.out.println(resultsWebPage);
The first example simple returns the id. The second only gave me an output of 1. The medication name I am entering returns 11 results into the whatDosage div
The div looks like this:
<div id="whatDosage" class="btn-group btn-group-vertical buttons-radio span12" role="radiogroup">
<input id="dosage1" class="hide" type="radio" value="20 MG Enteric Coated Capsule" name="dosage">
<button class="btn btn-large" rel="dosage1" role="radio" type="button">20 MG Enteric Coated Capsule</button>
<input id="dosage2" class="hide" type="radio" value="30 MG Enteric Coated Capsule" name="dosage">
<button class="btn btn-large" rel="dosage2" role="radio" type="button">30 MG Enteric Coated Capsule</button>
<input id="dosage3" class="hide" type="radio" value="60 MG Enteric Coated Capsule" name="dosage">
<button class="btn btn-large" rel="dosage3" role="radio" type="button">60 MG Enteric Coated Capsule</button>
</div>
The input elements are hidden with the button elements being what are displayed on screen.
This could be resolved by changing your element locating strategy.
I am assuming a skeleton for your webpage to be something as below. Though this may not be accurate, it will give you an idea of the approach to use.
When you run your code (2):
what it does is that it selects the “whatDosage” div and not the child elements which you require. Now since there is one “whatDosage” div, that explains that doing .size() returns 1.
You should be doing something on the lines of this:
This will give you the following output:
You will want to change the strategy to locate the strength/form elements as per your code. Some apporoached are By.id, By.className, By.xpath, etc. and can refer to the selenium docs for Locating UI elements.