为什么 ActionChains 在 PhantomJS Webdriver 中不起作用,但在 Chrome Webdriver 中起作用?

Why ActionChains does not work in PhantomJS Webdriver but works in Chrome Webdriver?

我想使用 PhantomJS 更改滑块手柄,但它不起作用。请检查以下在 Chrome Webdriver 下有效但在 PhantomJS 中无效的代码。这些代码尝试将手柄向右滑动滑块宽度的 10%。 有什么想法让它适用于 PhantomJS 吗?

from browsermobproxy import Server
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains

# settings for choosing Chrome or PhantomJS:
#...
#...
#driver = webdriver.Chrome(...) or webdriver.PhantomJS(...)

url = "http://jqueryui.com/slider/"
driver.get(url)
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.TAG_NAME, "iframe")))

handle_element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#slider > span")))
bar_element = driver.find_element_by_css_selector("#slider")
offset = bar_element.size['width'] * 0.1

print "Initial handle location:", handle_element.get_attribute("style")

move = ActionChains(driver)
move.click_and_hold(handle_element).move_by_offset(offset, 0).release().perform()

print "Handle location after sliding:", handle_element.get_attribute("style")

print "Slider bar size:", bar_element.size['width']

驱动程序为Chrome时的输出:

Initial handle location: left: 0%;
Handle location after sliding: left: 10%;
Slider bar size: 548

驱动程序为 PhantomJS 时的输出:

Initial handle location: left: 0%;
Handle location after sliding: left: 0%;
Slider bar size: 444

PhantomJS 本身不支持拖动。您将不得不尝试合成事件。

For 'mousemove', however, there is no button pressed (i.e. it is not dragging)

Source

如果页面上有 jQuery,您可以尝试类似 this through driver.execute_script(). Here 的示例 ruby。