@FindBy单选按钮的注释

时间:2016-10-05 04:09:19

标签: java selenium selenium-webdriver

我必须使用@FindBy选择单选按钮类型中的一个值。 这是我需要自动化的html代码。

我尝试过@FindBy(how=How.CSS,using= "[type='radio']" and value) 但我必须动态传入值。即。选择BlueSkies或Pangea航空公司。编写它的最佳方式是什么。将@FindBy返回WebElements列表,我可以像这样工作。请帮忙。

   <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>

2 个答案:

答案 0 :(得分:3)

@FindBy(how = How.CSS, using = "[value*='Blue Skies Airlines']") private WebElement blueSkyesRadioButton; @FindBy(how = How.CSS, using = "[value*='Pangea Airlines']") private WebElement pangeaAirlinesRadioButton; 中的值无法动态显示。您可以将其拆分为两个

@FindBy(how = How.CSS, using = "[type='radio']")
private List<WebElement> radioButtons;

或者将它们插入列表并使用索引

.myButton {
    -moz-box-shadow:inset 0px 1px 0px 0px #ffffff;
    -webkit-box-shadow:inset 0px 1px 0px 0px #ffffff;
    box-shadow:inset 0px 1px 0px 0px #ffffff;
    background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #f9f9f9), color-stop(1, #e9e9e9));
    background:-moz-linear-gradient(top, #f9f9f9 5%, #e9e9e9 100%);
    background:-webkit-linear-gradient(top, #f9f9f9 5%, #e9e9e9 100%);
    background:-o-linear-gradient(top, #f9f9f9 5%, #e9e9e9 100%);
    background:-ms-linear-gradient(top, #f9f9f9 5%, #e9e9e9 100%);
    background:linear-gradient(to bottom, #f9f9f9 5%, #e9e9e9 100%);
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#f9f9f9', endColorstr='#e9e9e9',GradientType=0);
    background-color:#f9f9f9;
    -moz-border-radius:6px;
    -webkit-border-radius:6px;
    border-radius:6px;
    border:1px solid #dcdcdc;
    display:inline-block;
    cursor:pointer;
    color:#666666;
    font-family:Arial;
    font-size:10px;
    font-weight:bold;
    padding:6px 24px;
    text-decoration:none;
    text-shadow:0px 1px 0px #ffffff;
}
.myButton:hover {
    background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #e9e9e9), color-stop(1, #f9f9f9));
    background:-moz-linear-gradient(top, #e9e9e9 5%, #f9f9f9 100%);
    background:-webkit-linear-gradient(top, #e9e9e9 5%, #f9f9f9 100%);
    background:-o-linear-gradient(top, #e9e9e9 5%, #f9f9f9 100%);
    background:-ms-linear-gradient(top, #e9e9e9 5%, #f9f9f9 100%);
    background:linear-gradient(to bottom, #e9e9e9 5%, #f9f9f9 100%);
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#e9e9e9', endColorstr='#f9f9f9',GradientType=0);
    background-color:#e9e9e9;
}
.myButton:active {
    position:relative;
    top:1px;
}

答案 1 :(得分:0)

public void selectRadio(String airlineName){
  for(WebElement radio : radioButtons){
    if(radio.getAttribute("value").contains(airlineName)
        radio.click();
  }
}

支持解决方案的更多解释由@Guy提供,您可以编写一种方法来动态选择无线电。例如

setVelocity

如果您愿意,可以使用迭代器。您只需要通过页面对象调用此方法,将airlineName作为参数传递。