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

时间:2015-04-24 11:41:21

标签: python-2.7 selenium-webdriver webdriver phantomjs selenium-chromedriver

我想使用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

1 个答案:

答案 0 :(得分:2)

PhantomJS并不支持本地拖动。你将不得不尝试合成事件。

  

对于' mousemove'但是,没有按下按钮(即它没有拖动)

Source

如果页面上有jQuery,您可以尝试使用thisdriver.execute_script()之类的内容。 Here是ruby的一个例子。