Ruby Selenium WebDriver拖放 [英] Ruby selenium webdriver drag and drop
问题描述
我正在尝试使用Selenium Webdriver和ruby执行拖放操作,并且尝试了以下选项:
I am trying to perform drag and drop operation using selenium webdriver and ruby, and I tried the following options:
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
选项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
选项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
当我执行@driver.find_element(:css, "div.captcha div.sliderCaptcha div.arrow").click
或@driver.find_element(:css, "div.captcha div.sliderCaptcha div.dottedBorder").click
时,代码可以单击对象,即它可以找到元素,但由于某些原因而无法拖放.上述拖放选项均无效.
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.
推荐答案
我在拖放方面取得了很大的成功,但是发现我不得不将拖放操作分成几个动作才能使其工作.我的应用程序.
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.
例如,我必须在首次点击后入睡500毫秒,因为javascript要求用户在这段时间内按住不放,然后才能进行拖放操作.另外,我必须在初始拖动后重新定位放置目标,因为它仅在拖动开始后才出现.
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
希望这突出显示了在某些网站中,拖放操作不像使用内置的drag_and_drop方法那么简单,并且可以拆分动作构建器
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
这篇关于Ruby Selenium WebDriver拖放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!