可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
If I want to select an option of a dropdown box, there are several ways to do that. I always used:
driver.findElement(By.id("selection")).sendKeys("Germany");
But that didn't work every time. Sometimes another option was selected. So I googled a little bit and found this piece of code which works every time:
WebElement select = driver.findElement(By.id("selection"));
List<WebElement> options = select.findElements(By.tagName("option"));
for (WebElement option : options) {
if("Germany".equals(option.getText()))
option.click();
}
But that works really really slow. If I have a long list with many items in it, it really takes too much time. So my question is, is there a solution which works every time and is fast?
回答1:
You could try this:
IWebElement dropDownListBox = driver.findElement(By.Id("selection"));
SelectElement clickThis = new SelectElement(dropDownListBox);
clickThis.SelectByText("Germany");
回答2:
Try the following:
import org.openqa.selenium.support.ui.Select;
Select droplist = new Select(driver.findElement(By.Id("selection")));
droplist.selectByVisibleText("Germany");
回答3:
Try the Select helper class and see if that makes any difference?
String valueToSelect= "Germany";
WebElement select = driver.findElement(By.id("selection"));
Select dropDown = new Select(select);
String selected = dropDown.getFirstSelectedOption().getText();
if(selected.equals(valueToSelect)) {//do stuff already selected}
List<WebElement> Options = dropDown.getOptions();
for(WebElement option:Options){
if(option.getText().equals(valueToSelect)){
option.click();
}
}
回答4:
For some strange reason the SelectElement
for webdriver (version 2.25.1.0) does not properly work with the firefoxdriver (Firefox 15). Sometimes it may not select an option from a dropdownlist. It does, however, seem to work with the chromedriver... This is a link to the chromedriver... just drop it in the bin dir.
回答5:
Example for select an option from the drop down list:
Click on drop down list by using id or csspath or xpath or name. I have used id here.
driver.findElement(By.id("dropdownlistone")).click(); // To click on drop down list
driver.findElement(By.linkText("india")).click(); // To select a data from the drop down list.
回答6:
I have to struggle to find how to achieve especial those who are new to this tool (like me)
C# code:
IWebElement ddl = ffDriver.FindElement(By.Id("ddlGoTo"));
OpenQA.Selenium.Support.UI.SelectElement clickthis = new OpenQA.Selenium.Support.UI.SelectElement(ddl);
clickthis.SelectByText("Your Text");
hope this help others!
回答7:
public static void mulptiTransfer(WebDriver driver, By dropdownID, String text, By to)
{
String valuetext = null;
WebElement element = locateElement(driver, dropdownID, 10);
Select select = new Select(element);
List<WebElement> options = element.findElements(By.tagName("option"));
for (WebElement value: options)
{
valuetext = value.getText();
if (valuetext.equalsIgnoreCase(text))
{
try
{
select.selectByVisibleText(valuetext);
locateElement(driver, to, 5).click();
break;
}
catch (Exception e)
{
System.out.println(valuetext + "Value not found in Dropdown to Select");
}
}
}
}
回答8:
select = driver.FindElement(By.CssSelector("select[uniq id']"));
selectElement = new SelectElement(select);
var optionList =
driver.FindElements(By.CssSelector("select[uniq id']>option"));
selectElement.SelectByText(optionList[GenerateRandomNumber(1, optionList.Count())].Text);
回答9:
Just wrap your WebElement into Select Object as shown below
Select dropdown = new Select(driver.findElement(By.id("identifier")));
Once this is done you can select the required value in 3 ways. Consider an HTML file like this
<html>
<body>
<select id = "designation">
<option value = "MD">MD</option>
<option value = "prog"> Programmer </option>
<option value = "CEO"> CEO </option>
</option>
</select>
<body>
</html>
Now to identify dropdown do
Select dropdown = new Select(driver.findElement(By.id("designation")));
To select its option say 'Programmer' you can do
dropdown.selectByVisibleText("Programmer ");
or
dropdown.selectByIndex(1);
or
dropdown.selectByValue("prog");
Happy Coding :)
回答10:
You can use this
(new SelectElement(driver.FindElement(By.Id(""))).SelectByText("");