我正在使用以下HTML:
Select a number:
<select onchange="updatesum()" name="s1">
<option selected="">0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4 </option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9 </option>
</select>
我想从此HTML中选择任意数字(包含字符串格式的数字),将其分配给int
变量。
我使用的方法是:
以上2个步骤的代码如下:
WebElement drop = dr.findElement(By.xpath(".//select[@name='s1']"));
Select num1 = new Select(drop);
num1.selectByIndex(2);
String val1 = dr.findElement(By.xpath(".//select[@name='s1']")).getText();
当我运行它时,它会返回下拉列表中的所有值,因此我无法将其转换为int。我只需要getText()
返回选定的值。
请指教
答案 0 :(得分:0)
使用getFirstSelectOption()
method:
String val1 = num1.getFirstSelectOption().getText();