0

我有一个包含 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]

如何在页面加载时捕获默认值。它可能是它们中的任何一个,并且每次我想捕获组合框中默认显示的值时

4

1 回答 1

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();
于 2015-09-16T07:31:18.953 回答