可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm new to both C# and Selenium WebDriver.
I know how to select/click on an option in a drop-down list, but I've a problem before that. Since the drop-down list is dynamically generated, I have to get all options/values from the list before running each case.
Is there anyone kindly tell me how to get all values/options from a drop-down list. I'm using IE and I didn't find any class which supports method to get values/options in Selenium.IE namespace for C#.
My example:
A list contains several time zones:
<TD>
<select name = "time_zone">
<option value "-09:00"><script>timezone.Alaska</script></option>
<option value "+00:00"><script>timezone.England</script></option>
<option value "+02:00"><script>timezone.Greece</script></option>
<option value "+05:30"><script>timezone.India</script></option>
</select>
<TD>
This is a drop-down list in an IE page and how to get the dynamically generated time zone list?
My code:
IWebElement elem = driver.FindElement(By.XPath("//select[@name='time_zone']"));
List<IWebElement> options = elem.FindElements(By.TagName("option"));
C# just pops an Error:
Cannot implicitly covert type 'OpenQA.Selenium.IWebElement' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?).
thanks.
回答1:
You can try using the WebDriver.Support SelectElement found in OpenQA.Selenium.Support.UI.Selected namespace to access the option list of a select list:
IWebElement elem = driver.FindElement(By.XPath("//select[@name='time_zone']"));
SelectElement selectList = new SelectElement(elem);
IList<IWebElement> options = selectList.Options;
You can then access each option as an IWebElement, such as:
IWebElement firstOption = options[0];
Assert.AreEqual(firstOption.GetAttribute("value"), "-09:00");
回答2:
Select select = new Select(driver.findElement(By.id("searchDropdownBox")));
select.getOptions();//will get all options as List<WebElement>
回答3:
Make sure you reference the WebDriver.Support.dll assembly to gain access to the OpenQA.Selenium.Support.UI.SelectElement dropdown helper class. See this thread for additional details.
Edit: In this screenshot, you can see that I can get the options just fine. Is IE opening up when you create a new InternetExplorerDriver?
回答4:
Here is code in Java to get all options in dropdown list.
WebElement sel = myD.findElement(By.name("dropdown_name"));
List<WebElement> lists = sel.findElements(By.tagName("option"));
for(WebElement element: lists)
{
String var2 = tdElement.getText();
System.out.println(var2);
}
Hope it may helpful to someone.
回答5:
You can use selenium.Support
to use the SelectElement
class, this class have a property "Options" that is what you are looking for, I created an extension method to convert your web element to a select element
public static SelectElement AsDropDown(this IWebElement webElement)
{
return new SelectElement(webElement);
}
then you could use it like this
var elem = driver.FindElement(By.XPath("//select[@name='time_zone']"));
var options = elem.AsDropDown().Options
回答6:
Use IList<IWebElement>
instead of List<IWebElement>
.
For instance:
IList<IWebElement> options = elem.FindElements(By.TagName("option"));
foreach (IWebElement option in options)
{
Console.WriteLine(option.Text);
}
回答7:
To get all the dropdown values you can use List.
List<string> lstDropDownValues = new List<string>();
int iValuescount = driver.FindElement(By.Xpath("\html\....\select\option"))
for(int ivalue = 1;ivalue<=iValuescount;ivalue++)
{
string strValue = driver.FindElement(By.Xpath("\html\....\select\option["+ ivalue +"]"));
lstDropDownValues.Add(strValue);
}
回答8:
WebElement drop_down =driver.findElement(By.id("Category"));
Select se = new Select(drop_down);
for(int i=0 ;i<se.getOptions().size(); i++)
System.out.println(se.getOptions().get(i).getAttribute("value"));
回答9:
WebElement element = driver.findElement(By.id("inst_state"));
Select s = new Select(element);
List <WebElement> elementcount = s.getOptions();
System.out.println(elementcount.size());
for(int i=0 ;i<elementcount.size();i++)
{
String value = elementcount.get(i).getText();
System.out.println(value);
}
回答10:
To get all options in a drop-down list by Selenium WebDriver C#:
SelectElement TimeZoneSelect = new SelectElement(driver.FindElement(By.Name("time_zone")));
IList<IWebElement> ElementCount = TimeZoneSelect.Options;
int ItemSize = ElementCount.Count;
for (int i = 0; i < ItemSize; i++)
{
String ItemValue = ElementCount.ElementAt(i).Text;
Console.WriteLine(ItemValue);
}
回答11:
It seems to be a cast exception. Can you try converting your result to a list
i.e. elem.findElements(xx).toList
?