我有一个包含2个值的组合框(比如Xreg和MBA)。 默认情况下,只会根据搜索条件显示一个值(Xreg或MBA)。 Xreg的xpath是
/html/body/div/div[4]/div[2]/form/div[1]/table[2]/tbody/tr[1]/td[2]/select/option[2]
和MBA
/html/body/div/div[4]/div[2]/form/div[1]/table[2]/tbody/tr[1]/td[2]/select/option[3]
如何捕获页面加载时的默认值。它可能是其中之一,每次我想捕获默认情况下在组合框中显示的值
答案 0 :(得分:1)
您可以使用Selenium的Select类:
// this is only an example with the code provided, usually the select element has an id and you wouldn't necessarily need xpath
By locatorToYourSelectElement = By.xpath("/html/body/div/div[4]/div[2]/form/div[1]/table[2]/tbody/tr[1]/td[2]/select");
WebElement selectElement = driver.findElement(locatorToYourSelectElement);
Select dropdown = new Select(selectElement);
// Supposing you do not have multiple selection you will get the displayed element now very easily:
WebElement currentlySelectedOption = dropdown.getFirstSelectedOption();