I have to select one of the several values of type radio button using @FindBy
. Here is my html code that I need to automate.
I tried giving @FindBy(how=How.CSS,using= "[type='radio']" and value)
But I have to pass in the value dynamically. ie. Chose BlueSkies or Pangea airlines.What is the best way to write it.Will @FindBy
return a list of WebElements and can I work on it like that. Please help.
<input type="radio" name="outFlight" value="Blue Skies Airlines$361$271$7:10"/>
</td>
<td class="data_left" align="LEFT" bgcolor="CCCCCC">
<font size="-1" face="ARIAL">
<b>Blue Skies Airlines 361</b>
</font>
</td>
<td class="data_center_mono" align="CENTER" valign="TOP" bgcolor="CCCCCC">
<font size="-1" face="ARIAL">7:10</font>
<br/>
</td>
<td class="data_center" align="CENTER" bgcolor="CCCCCC" nowrap="">
<font size="-1" face="ARIAL">non-stop</font>
</td>
</tr>
<tr>
<td class="data_verb_xcols" valign="top" bgcolor="#FFFFFF" colspan="4">
</tr>
<tr>
<td class="frame_action_xrows" rowspan="2" align="CENTER" bgcolor="#FFCC00" valign="CENTER">
<input type="radio" name="outFlight" value="Pangea Airlines$362$274$9:17"/>
</td>
<td class="data_left" align="LEFT" bgcolor="CCCCCC">
<font size="-1" face="ARIAL">
<b>Pangaea Airlines 362</b>
</font>
</td>
The value in @FindBy
can't be dynamic. You can split it to two
@FindBy(how = How.CSS, using = "[value*='Blue Skies Airlines']")
private WebElement blueSkyesRadioButton;
@FindBy(how = How.CSS, using = "[value*='Pangea Airlines']")
private WebElement pangeaAirlinesRadioButton;
Or to insert them both to list and work with indexes
@FindBy(how = How.CSS, using = "[type='radio']")
private List<WebElement> radioButtons;
@FindBy(how = How.CSS, using = "[type='radio']")
private List<WebElement> radioButtons;
Some more explanation supporting the solution give by @Guy, you can write an method to select radio dynamically. For an example
public void selectRadio(String airlineName){
for(WebElement radio : radioButtons){
if(radio.getAttribute("value").contains(airlineName)
radio.click();
}
}
You can use iterator if you wish. You just need to call this method by page object passing the airlineName as an argument.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.