[英]Get Current Value of Select Field
我有一個網站正在嘗試測試,其中包含多個下拉列表。 一個選擇元素如下:
<select class="input-xxlarge" name="dosage">
<option value="20 MG, Enteric Coated Capsule">20 MG, Enteric Coated Capsule</option>
<option value="60 MG, Enteric Coated Capsule">60 MG, Enteric Coated Capsule</option>
<option value="30 MG, Enteric Coated Capsule">30 MG, Enteric Coated Capsule</option>
</select>
更改值后,我唯一注意到的變化是類名稱更改為:“ input-xxlargechangedInput”。
我正在使用WebDriver進行測試,並希望獲取當前值。 我嘗試執行以下操作:
WebElement strengthForm = driver.findElement(By.xpath("/html/body/section/article[2]/div/table/tbody/tr["+rowNum2+"]/td/div/article/section/form/fieldset/label/select"));
String strengthBeforeChoosing = strengthForm.getText().toString();
System.out.println(strengthBeforeChoosing);
問題在於,這給了我所有選擇,而不是獲取當前值。
20毫克腸溶膠囊60毫克腸溶膠囊30毫克腸溶膠囊
我將如何獲取下拉列表的當前值,以便在更改后將其與當前值進行比較?
嘗試使用:
Select select = new Select(driver.findElement(By.id("dosage")));
String selectedText = select.getFirstSelectedOption().getText();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.