2011-03-11 40 views
51

Tôi đang thử nghiệm web của mình bằng cách chọn một tùy chọn. Bạn có thể tìm thấy ví dụ tại đây: http://www.tizag.com/phpT/examples/formex.phpLàm thế nào để chọn một tùy chọn từ trình đơn thả xuống bằng Selenium WebDriver C#?

Mọi thứ đều hoạt động tốt ngoại trừ việc chọn phần tùy chọn. Cách chọn tùy chọn theo giá trị hoặc theo nhãn?

Mã của tôi:

using OpenQA.Selenium.Firefox; 
using OpenQA.Selenium; 
using System.Collections.ObjectModel; 
using System.Text.RegularExpressions; 
using System.Threading; 
using System.Diagnostics; 
using System.Runtime.InteropServices; 

class GoogleSuggest 
{ 
    static void Main() 
    { 
     IWebDriver driver = new FirefoxDriver(); 

     //Notice navigation is slightly different than the Java version 
     //This is because 'get' is a keyword in C# 
     driver.Navigate().GoToUrl("http://www.tizag.com/phpT/examples/formex.php"); 
     IWebElement query = driver.FindElement(By.Name("Fname")); 
     query.SendKeys("John"); 
     driver.FindElement(By.Name("Lname")).SendKeys("Doe"); 
     driver.FindElement(By.XPath("//input[@name='gender' and @value='Male']")).Click(); 
     driver.FindElement(By.XPath("//input[@name='food[]' and @value='Chicken']")).Click(); 
     driver.FindElement(By.Name("quote")).Clear(); 
     driver.FindElement(By.Name("quote")).SendKeys("Be Present!"); 
     driver.FindElement(By.Name("education")).SendKeys(Keys.Down + Keys.Enter); // working but that's not what i was looking for 
     // driver.FindElement(By.XPath("//option[@value='HighSchool']")).Click(); not working 
     // driver.FindElement(By.XPath("/html/body/table[2]/tbody/tr/td[2]/table/tbody/tr/td/div[5]/form/select/option[2]")).Click(); not working 
     // driver.FindElement(By.XPath("id('examp')/x:form/x:select[1]/x:option[2]")).Click(); not working 

     } 
} 

Trả lời

113

Bạn phải tạo một đối tượng yếu tố lựa chọn từ danh sách thả xuống.

using OpenQA.Selenium.Support.UI; 

// select the drop down list 
var education = driver.FindElement(By.Name("education")); 
//create select element object 
var selectElement = new SelectElement(education); 

//select by value 
selectElement.SelectByValue("Jr.High"); 
// select by text 
selectElement.SelectByText("HighSchool"); 

Thông tin thêm here

+0

công trình như một say mê cảm ơn! làm cho mọi việc nhanh hơn cho các bài kiểm tra của tôi! – motto

+0

Có lỗi. 'var selectElement = new SelectElement (giáo dục);' Nên là: 'var selectElement = new SelectElement (phần tử);' –

+1

@greg thanks, cố định rằng –

7

cách khác có thể là cái này:

driver.FindElement(By.XPath(".//*[@id='examp']/form/select[1]/option[3]")).Click(); 

và bạn có thể thay đổi chỉ số trong tùy chọn [x] thay đổi x bởi số lượng các yếu tố mà bạn muốn lựa chọn.

Tôi không biết nếu đó là cách tốt nhất nhưng tôi hy vọng rằng sẽ giúp bạn.

1

Đây là cách nó làm việc cho tôi (chọn kiểm soát bởi ID và lựa chọn bằng văn bản):

protected void clickOptionInList(string listControlId, string optionText) 
{ 
    driver.FindElement(By.XPath("//select[@id='"+ listControlId + "']/option[contains(.,'"+ optionText +"')]")).Click(); 
} 

sử dụng:

clickOptionInList("ctl00_ContentPlaceHolder_lbxAllRoles", "Tester"); 
0

Bạn chỉ cần phải vượt qua giá trị và nhập khóa:

driver.FindElement(By.Name("education")).SendKeys("Jr.High"+Keys.Enter); 
+0

Có khả năng sẽ thất bại nếu danh sách thả xuống chứa nhiều tùy chọn với các văn bản tương tự. – Antti

3

Thêm điểm vào điều này. Tôi đã gặp một vấn đề mà không gian tên OpenQA.Selenium.Support.UI không có sẵn sau khi cài đặt liên kết Selenium.NET vào dự án C#. Sau đó phát hiện ra rằng chúng ta có thể dễ dàng cài đặt phiên bản mới nhất của Lớp hỗ trợ Selenium WebDriver bằng cách chạy lệnh Cài đặt-gói Selenium.Support trong NuGet Package Manager Console.

3

Để chọn tùy chọn qua văn bản;

(new SelectElement(driver.FindElement(By.XPath(""))).SelectByText(""); 

Để Chọn một Tùy chọn qua Value:

(new SelectElement(driver.FindElement(By.XPath(""))).SelectByValue(""); 
0

Nếu bạn đang tìm kiếm chỉ bất kỳ lựa chọn từ hộp thả xuống, tôi cũng thấy "chọn bởi chỉ số" phương pháp rất hữu ích.

if (IsElementPresent(By.XPath("//select[@id='Q43_0']"))) 
{ 
    new SelectElement(driver.FindElement(By.Id("Q43_0")))**.SelectByIndex(1);** // This is selecting first value of the drop-down list 
    WaitForAjax(); 
    Thread.Sleep(3000); 
} 
else 
{ 
    Console.WriteLine("Your comment here); 
} 
1

Selenium WebDriver C# mã để lựa chọn mục từ trên xuống:

IWebElement EducationDropDownElement = driver.FindElement(By.Name("education")); 
SelectElement SelectAnEducation = new SelectElement(EducationDropDownElement); 

Có 3 cách để chọn thả xuống mục: i) Chọn bằng chữ ii) Chọn theo Index iii) Chọn theo giá trị gia tăng

Chọn bằng văn bản:

SelectAnEducation.SelectByText("College");//There are 3 items - Jr.High, HighSchool, College 

Chọn theo Index:

SelectAnEducation.SelectByIndex(2);//Index starts from 0. so, 0 = Jr.High 1 = HighSchool 2 = College 

Chọn theo giá trị gia tăng:

SelectAnEducation.SelectByValue("College");//There are 3 values - Jr.High, HighSchool, College 
0
var select = new SelectElement(elementX); 
select.MoveToElement(elementX).Build().Perform(); 

    var click = (
     from sel in select 
     let value = "College" 
     select value 
     ); 
+4

Vui lòng thêm giải thích cho mã của bạn: _why_ đó có phải là câu trả lời cho câu hỏi và _điều gì làm cho nó khác biệt_ so với các câu trả lời được đưa ra trước đó không? –

Các vấn đề liên quan