1

I would like to change a slider handle using PhantomJS but it does not work. Please check the following codes that work under Chrome Webdriver but not in PhantomJS. These codes try to slide the handle with the amount of 10% of slider bar's width to the right. Any idea to make it work for 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']

Output when driver is Chrome:

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

Output when driver is PhantomJS:

Initial handle location: left: 0%;
Handle location after sliding: left: 0%;
Slider bar size: 444
Jose
  • 336
  • 2
  • 4
  • 16
  • @ArtjomB. thank you for your comment. I am a beginner. So, could you please let me know what "synthetic events" are? – Jose Apr 24 '15 at 11:45
  • Sorry, didn't receive your notification, because I deleted my comment and made it into an answer. If you have jQuery on the page, you can try something like [this](http://stackoverflow.com/questions/21588122/move-draggable-to-position-programmatically) through `driver.execute_script()`. – Artjom B. Apr 24 '15 at 12:40

1 Answers1

2

PhantomJS doesn't support dragging natively. You will have to try with synthetic events.

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

Source

If you have jQuery on the page, you can try something like this through driver.execute_script(). Here is an example with ruby.

Community
  • 1
  • 1
Artjom B.
  • 61,146
  • 24
  • 125
  • 222