问题
I am trying to perform drag and drop operation using selenium webdriver and ruby, and I tried the following options:
Option 1: based on http://rubydoc.info/gems/selenium-webdriver/0.0.28/Selenium/WebDriver/Element#drag_and_drop_on-instance_method
el1 = @driver.find_element(:css, "div.captcha div.sliderCaptcha div.arrow")
el2 = @driver.find_element(:css, "div.captcha div.sliderCaptcha div.dottedBorder")
el1.drag_and_drop_on el2
Option 2:
el1 = @driver.find_element(:css, "div.captcha div.sliderCaptcha div.arrow")
el2 = @driver.find_element(:css, "div.captcha div.sliderCaptcha div.dottedBorder")
@driver.action.drag_and_drop(el1, el2).perform
Option 3:
el1 = @driver.find_element(:css, "div.captcha div.sliderCaptcha div.arrow")
el2 = @driver.find_element(:css, "div.captcha div.sliderCaptcha div.dottedBorder")
@browser.action.drag_and_drop(element, target).perform
when I do @driver.find_element(:css, "div.captcha div.sliderCaptcha div.arrow").click or @driver.find_element(:css, "div.captcha div.sliderCaptcha div.dottedBorder").click the code is able to click object, i.e. it is able to find element but due to some reason not able to drag and drop. None of above drag and drop option worked.
回答1:
I have had quite a lot of success with drag and drop, but found that I had to split my drag and drop into several actions to allow it to work in my app.
For example, I had to sleep 500 ms after the initial click as the javascript required the user to hold down for that period before allowing drag and drop to occur. Also, I had to refind the drop target after the initial drag as it only appeared after the drag started.
I am not saying this is occuring in your application, but it may give you some hints.
For example, my code looked something like this;
el1 = @driver.find_element(:css, "div.captcha div.sliderCaptcha div.arrow")
el_in_between = @driver.find_element(:css, "div.something")
@browser.action.click_and_hold(el1).perform
sleep 0.5
@browser.action.move_to(el_in_between).perform
el2 = @driver.find_element(:css, "div.captcha div.sliderCaptcha div.dottedBorder")
@browser.action.move_to(el2).release.perform
Hopefully this highlights that in some websites, drag and drop is not quite as simple as using a built in drag_and_drop method and that an action builder can be split
来源:https://stackoverflow.com/questions/20722831/ruby-selenium-webdriver-drag-and-drop