在您的第一个选项中,硒明确指出 ,这意味着您在提供xpath
for的option
同时仅xpath
希望选择。
不需要使用您提供的其他选项,只需使用以下第一个选项即可:-
Select elm = new Select(driver.findElement(By.id("type")));
elm.selectByVisibleText("Date");
要么 ByIndex
elm.selectByIndex(2);
要么 ByValue
elm.selectByValue("1");
如果不幸的是您的第一个选项不起作用,我希望您使用第三个选项使用JavascriptExecutor
,如下所示:-
WebElement select = driver.findElement(By.id("type"));
((JavascriptExecutor)driver).executeScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", select, "Date");
希望它能帮助您… :)