我正在尝试使用Selenium WebDriver和Ruby实现拖放功能。为了实现我的目标:
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :firefox
driver.get "myurl"
source = driver.find_element(:id, '19026240')
target = driver.find_element(:xpath, "//div[@id='rootLabelChildren']/div/div")
driver.action.drag_and_drop(source, target).perform
这不会导致任何错误,如无法定位元素,元素未附加到DOM等。但这会导致超时错误:
Timeout::Error: Timeout::Error
from C:/Ruby193/lib/ruby/1.9.1/net/protocol.rb:146:in `rescue in rbuf_fill'
from C:/Ruby193/lib/ruby/1.9.1/net/protocol.rb:140:in `rbuf_fill'
from C:/Ruby193/lib/ruby/1.9.1/net/protocol.rb:122:in `readuntil'
from C:/Ruby193/lib/ruby/1.9.1/net/protocol.rb:132:in `readline'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:2562:in `read_status_line'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:2551:in `read_new'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1319:in `block in transport_request'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1316:in `catch'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1316:in `transport_request'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1293:in `request'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1286:in `block in request'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:745:in `start'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1284:in `request'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/default.rb:83:in `response_for
'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/default.rb:39:in `request'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/common.rb:40:in `call'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/bridge.rb:629:in `raw_execute'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/bridge.rb:607:in `execute'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/bridge.rb:394:in `mouseMoveTo'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/common/mouse.rb:54:in `move_to'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/common/action_builder.rb:344:in `block in
perform'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/common/action_builder.rb:343:in `each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/common/action_builder.rb:343:in `perform'
from (irb):6
from C:/Ruby193/bin/irb:12:in `<main>'irb(main):007:0> end
SyntaxError: (irb):7: syntax error, unexpected keyword_end
from C:/Ruby193/bin/irb:12:in `<main>'
手动,拖放工作正常。什么可能是超时错误的原因?
Firefox版本:21 Selenium WebDriver:2.33.0
HTML code:
目标
<div id="rootLabelChildren" class="children">
<div class="label " labelid="74ce292d65844af7b4e480782d8e9b38" labelname="0106">
<div class="labelName terminal" draggable="true">0106</div>
<div class="children"> </div>
</div>
对于来源
<table class="dataGridHeader">
<div class="dataGridScrollbarContainer scrollable" style="">
<div class="dataGridContents" style="min-height: 12880px;">
<table class="buffer" style="margin-top: 0px;">
<tbody>
<tr id="19026240" class="selected" draggable="true">
答案 0 :(得分:0)
Selenium WebDriver:2.33.0 不支持 Firefox版本:21 。因此将其降级为 Firefox版本:19 或 Firefox版本:20 。
这将处理错误:rescue in rbuf_fill
。
要拖放元素,请尝试以下操作:
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :firefox
driver.get "myurl"
source = driver.find_element(:css, 'tr#19026240')
target = driver.find_element(:css, 'div#rootLabelChildren>div>div')
driver.action.drag_and_drop(source, target).perform
对于拖放功能,我得到了一个很好的链接HTML5 Drag and Drop with Selenium Webdriver。它会帮助你。