2012-03-05 23 views
11

Tôi mới dùng cả C# và Selenium WebDriver.Làm thế nào để có được tất cả các tùy chọn trong danh sách thả xuống bởi Selenium WebDriver sử dụng C#?

Tôi biết cách chọn/nhấp vào một tùy chọn trong danh sách thả xuống, nhưng tôi đã gặp sự cố trước đó. Vì danh sách thả xuống được tạo động, tôi phải nhận tất cả các tùy chọn/giá trị từ danh sách trước khi chạy từng trường hợp.

Có ai vui lòng cho tôi biết cách nhận tất cả các giá trị/tùy chọn từ danh sách thả xuống không. Tôi đang sử dụng IE và tôi không tìm thấy bất kỳ lớp nào hỗ trợ phương thức để lấy các giá trị/tùy chọn trong không gian tên Selenium.IE cho C#.

dụ của tôi: Một danh sách chứa một số múi giờ:

<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> 

Đây là một danh sách thả xuống trong một trang IE và làm thế nào để có được danh sách múi giờ tự động tạo ra?

Mã của tôi:

IWebElement elem = driver.FindElement(By.XPath("//select[@name='time_zone']")); 
List<IWebElement> options = elem.FindElements(By.TagName("option")); 

C# chỉ bật một Lỗi: Không thể ngầm bí mật kiểu 'OpenQA.Selenium.IWebElement' thành 'System.Collections.Generic.List'. Một chuyển đổi rõ ràng tồn tại (bạn có bỏ lỡ một diễn viên không?).

cảm ơn.

Trả lời

6

Đảm bảo bạn tham khảo hội thảo WebDriver.Support.dll để truy cập vào lớp trợ giúp thả xuống OpenQA.Selenium.Support.UI.SelectElement. Xem this thread để biết thêm chi tiết.

Chỉnh sửa: Trong ảnh chụp màn hình này, bạn có thể thấy rằng tôi có thể nhận các tùy chọn tốt. IE có mở khi bạn tạo InternetExplorerDriver mới không? Screenshot

+0

Tôi đã thêm WebDriver.dll và WebDriver.Support.dll vào tham khảo. –

+0

anyway, cảm ơn tất cả các bạn. Tôi vẫn đang tìm kiếm giải pháp. –

+0

Mã của bạn nên hoạt động nếu bạn sử dụng gõ ẩn: var options = elem.FindElements (By.TagName ("option")); –

0

Có vẻ như đó là ngoại lệ truyền. Bạn có thể thử chuyển đổi kết quả của mình thành danh sách tức là elem.findElements (xx) .toList không?

+0

Vâng, tôi đã cố gắng theo cách này. Nhưng khi tôi in các phần tử của Danh sách, tất cả phần tử của List [i] là "OpenQA.Selenium.Remote.RemoteWebElement", là kiểu nhưng không phải là giá trị. –

+0

Cảm ơn tất cả các bạn. Tôi tìm thấy một phương pháp trong không gian tên Selenium được gọi là "GetSelectOptions", nhưng tôi đã không làm thế nào để sử dụng nó với lớp IWebElement hoặc InternetExplorerWebDriver. –

11

Bạn có thể thử bằng cách sử dụng WebDriver.Support SelectElement tìm thấy trong OpenQA.Selenium.Support.UI.Selected namespace để truy cập danh sách lựa chọn của một danh sách lựa chọn:

IWebElement elem = driver.FindElement(By.XPath("//select[@name='time_zone']")); 

SelectElement selectList = new SelectElement(elem); 
IList<IWebElement> options = selectList.Options; 

Bạn có thể sau đó truy cập mỗi tùy chọn như một IWebElement, chẳng hạn như:

IWebElement firstOption = options[0]; 
Assert.AreEqual(firstOption.GetAttribute("value"), "-09:00"); 
1

Sử dụng IList<IWebElement> thay vì List<IWebElement>.

Ví dụ:

IList<IWebElement> options = elem.FindElements(By.TagName("option")); 
foreach (IWebElement option in options) 
{ 
    Console.WriteLine(option.Text); 
} 
3

Đây là mã trong Java để có được tất cả các tùy chọn trong danh sách thả xuống.

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); 
    } 

Hy vọng nó có thể hữu ích cho người khác.

7
Select select = new Select(driver.findElement(By.id("searchDropdownBox"))); 

select.getOptions();//will get all options as List<WebElement> 
0
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); 
} 
0
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")); 
0
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); 

     } 
2

Bạn có thể sử dụng selenium.Support sử dụng lớp SelectElement, lớp này có một tài sản "Tùy chọn" đó là những gì bạn đang tìm kiếm, tôi đã tạo ra một phương pháp khuyến nông để chuyển đổi của bạn phần tử web cho một phần tử chọn

public static SelectElement AsDropDown(this IWebElement webElement) 
{ 
    return new SelectElement(webElement); 
} 

thì bạn có thể sử dụng nó như thế này

var elem = driver.FindElement(By.XPath("//select[@name='time_zone']")); 
var options = elem.AsDropDown().Options 
0

Để có được tất cả các tùy chọn trong một danh sách thả xuống của 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); 
} 
Các vấn đề liên quan