2011-01-12 12 views
29

Tôi đang cố gắng làm quen với trình điều khiển selenium-web ruby ​​mới vì nó xuất hiện trực quan hơn so với phiên bản trước của selen và trình điều khiển ruby đi với nó. Ngoài ra, tôi đã gặp rắc rối khi sử dụng selenium cũ để làm việc với ruby ​​1.9.1 trong các cửa sổ vì vậy tôi nghĩ tôi sẽ tìm kiếm một giải pháp thay thế. Cho đến nay tôi đã làm điều này với kịch bản của tôi:Làm cách nào để đặt tùy chọn được chọn bằng ứng dụng Selenium WebDriver (selenium 2.0) trong ruby ​​

require "selenium-webdriver" 

driver = Selenium::WebDriver.for :firefox 
driver.get "https://example.com" 

element = driver.find_element(:name, 'username') 
element.send_keys "mwolfe" 
element = driver.find_element(:name, 'password') 
element.send_keys "mypass" 
driver.find_element(:id, "sign-in-button").click 
driver.find_element(:id,"menu-link-my_profile_professional_info").click 
driver.find_element(:id,"add_education_btn").click 
country_select = driver.find_element(:name, "address_country") 

Vì vậy, về cơ bản tôi đang đăng nhập vào trang web của tôi và cố gắng để thêm một mục giáo dục vào hồ sơ người dùng của tôi .. Tôi có một tham chiếu đến một hộp chọn với tùy chọn (trong biến country_select) và bây giờ tôi muốn chọn một tùy chọn với một giá trị nhất định .. Tôi không thấy làm thế nào để làm điều này trong khách hàng mới .. Điều duy nhất tôi có thể nghĩ đến làm là lặp qua tất cả các tùy chọn cho đến khi tôi tìm thấy cái tôi muốn, và sau đó gọi phương thức execute_script: http://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver/Driver.html#execute_script-class_method để đặt chỉ mục đã chọn.

Có cách nào khác để thực hiện việc này không? Trong api java cho selen 2.0/webdriver đây: http://seleniumhq.org/docs/09_webdriver.html có là một ví dụ để làm điều này

Select select = new Select(driver.findElement(By.xpath("//select"))); 
select.deselectAll(); 
select.selectByVisibleText("Edam"); 

Nó không xuất hiện rằng phiên bản ruby ​​có tính năng này mặc dù trừ khi tôi đang thiếu một cái gì đó. Mọi trợ giúp sẽ được đánh giá cao.

Trả lời

17

Xin lưu ý rằng không ai trong số các ý trên làm việc nữa. Element#selectElement#toggle đã không được chấp nhận.Bạn cần phải làm một cái gì đó như:

my_select.click 
my_select.find_elements(:tag_name => "option").find do |option| 
    option.text == value 
end.click 
34

Công bố đầy đủ ở đây: Tôi hoàn toàn không có kiến ​​thức làm việc về Ruby.

Tuy nhiên, tôi khá tốt với Selenium vì vậy tôi nghĩ tôi có thể trợ giúp. Tôi nghĩ rằng những gì bạn đang tìm kiếm là phương pháp select. Nếu ruby ​​là bất cứ điều gì giống như các trình điều khiển khác, bạn có thể sử dụng phương pháp chọn để nói với một trong các tùy chọn nó được chọn.

Về giả/java nó sẽ giống như thế này

WebElement select = driver.findElement(By.name("select")); 
    List<WebElement> options = select.findElements(By.tagName("option")); 
    for(WebElement option : options){ 
     if(option.getText().equals("Name you want")) { 
      option.click(); 
      break; 
     } 
    } 

Select đối tượng bạn có ở trên là thực sự trong một gói hỗ trợ đặc biệt. Nó chỉ tồn tại cho Java và Net vào lúc này (Jan 2011)

+0

Điều này dường như không hoạt động, tôi đã thử country_select = driver.find_element (: xpath, "// chọn [id = 'address_country']/option = 'Austria'") country_select.select và điều đó đã không không làm gì cả. Tôi nghĩ phương pháp chọn giống như gọi select() trong javascript (đặt tiêu điểm vào một trường và đánh dấu văn bản) –

+0

Bạn đã thử trước tiên lấy phần tử chọn, sau đó lặp qua các tùy chọn WebElement và gọi phương thức chọn trên WebElement mà bạn muốn ? – pnewhook

+1

pnewhook đã đúng. Đây là mã Ruby tương đương: https://gist.github.com/777516 Nếu chọn yếu tố # không hoạt động, có thể thử Element # toggle. Nếu bạn nghĩ rằng API quá thấp, bạn có thể muốn kiểm tra gem gem watir-webdriver như một API thay thế cho cùng một công nghệ. – jarib

6

pnewhook nhận nó nhưng tôi muốn gửi phiên bản ruby ​​ở đây để mọi người có thể nhìn thấy nó:

require "selenium-webdriver" 
driver = Selenium::WebDriver.for :firefox 
driver.manage.timeouts.implicit_wait = 10 
driver.get "https://mysite.com" 
country_select = driver.find_element(:id=> "address_country") 
options = country_select.find_elements(:tag_name=>"option") 
options.each do |el| 
    if (el.value == "USA") 
     el.select() 
     break 
    end 
end 
2

Đối với phiên bản mới nhất của Webdriver (RC3), bạn nên sử dụng "click()" thay vì setSelected(). . Ngoài ra option.getText() tương đương với ("Tên bạn muốn") nên được sử dụng thay vì option.getText() == "Tên bạn muốn" trong JAVA:

<!-- language: lang-java --> 
WebElement select = driver.findElement(By.name("select")); 
List<WebElement> options = select.findElements(By.tagName("option")); 
for(WebElement option : options){ 
    if(option.getText().equals("Name you want"){ 
     option.click(); 
     break; 
    } 
} 
1

của Ruby Mã với Ví dụ:

require "selenium-webdriver" 

driver = Selenium::WebDriver.for :ie 

driver.navigate.to "http://google.com" 
a=driver.find_element(:link,'Advanced search') 

a.click 


a=driver.find_element(:name,'num') 

options=a.find_elements(:tag_name=>"option") 
options.each do |g| 
    if g.text == "20 results" 
    g.click 
    break 
    end 
end 
6

Bạn có thể sử dụng XPath để tránh lặp:

String nameYouWant = "Name you want"; 
WebElement select = driver.findElement(By.id(id)); 
WebElement option = 
    select.findElement(By.xpath("//option[contains(text(),'" + nameYouWant + "')]")); 
option.click(); 

hoặc

WebElement option = 
    select.findElement(By.xpath("//option[text()='" + nameYouWant + "']")); 
2
require "selenium-webdriver" 
webdriver = Selenium::WebDriver.for :firefox 

driver.navigate.to url 

dropdown = webdriver.find_element(:id,dropdownid) 
return nil if dropdown.nil? 
selected = dropdown.find_elements(:tag_name,"option").detect { |option| option.attribute('text').eql? value} 
if selected.nil? then 
    puts "Can't find value in dropdown list" 
else 
    selected.click 
end 

Trong trường hợp của tôi, đây chỉ là một mẫu đang hoạt động.

13

Tôi không biết những gì Selenium phiên bản này đã về, nhưng có vẻ như đó là Chọn lớp pnewhook đề cập trong Selenium 2,20

http://selenium.googlecode.com/svn-history/r15117/trunk/docs/api/rb/Selenium/WebDriver/Support/Select.html

option = Selenium::WebDriver::Support::Select.new(@driver.find_element(:xpath => "//select")) 
option.select_by(:text, "Edam") 
+3

Đây là câu trả lời hay nhất! Không cần phải lặp qua các phần tử của riêng bạn, chưa kể phải xem xét: tag_name => 'option' hết lần này đến lần khác trong mã của bạn. Mẹo chuyên nghiệp: để dễ dàng truy cập vào lớp Chọn (ví dụ: Select.new thay vì Selenium :: WebDriver :: Support :: Select.new), sử dụng: bao gồm Selenium :: WebDriver :: Support –

+3

Nếu bạn muốn chọn theo giá trị, bạn cũng có thể làm điều đó: 'option.select_by (: value," edam ")' – chiborg

0
#SELECT FROM DROPDOWN IN RUBY USING SELENIUM WEBDRIVER 
#AUTHOR:AYAN DATE:14 June 2012 

require "rubygems" 
require "selenium-webdriver" 



    begin 
    @driver = Selenium::WebDriver.for :firefox 
    @base_url = "http://www.yoururl.com" 
    @driver.manage.timeouts.implicit_wait = 30 

    @driver.get "http://www.yoursite.com" 


    #select Urugway as Country 
    Selenium::WebDriver::Support::Select.new(@driver.find_element(:id, "country")).select_by(:text, "Uruguay") 

     rescue Exception => e 
     puts e 
     @driver.quit 


    end 
0

Cách đơn giản nhất tôi được tìm thấy là:

select_elem.find_element (: css, "option [value = 'some_value']"). Nhấp vào