Select 使用 Selenium Webdriver 从组合框中随机取值

Select a random value out of a combo box using Selenium Webdriver

如何使用 Selenium Webdriver select 从组合框中随机取值?

假设这是下面的代码:

WebElement comboBox;
comboBox = driver.findElement(By.xpath(".//*[@id='sponsorship_id']"));

试试这个:

WebElement comboBoxElemenet;
comboBoxElemenet = driver.findElement(By.xpath(".//*[@id='sponsorship_id']"));
Select comboBox= new Select(comboBoxElemenet );
int randomIndex = new Random().nextInt(comboBox.getOptions().size);
comboBox.selectByIndex(randomIndex);