6

I have been using Python Selenium for quite some time and I have been happy with it until I got this new requirement which I am supposed to set sliders on a web-page (here) to certain values and then let the page run its scripts to update the page with the results.

My problem is how to set the slider min and max knobs () using Python Selenium. I have the tried the example here and my code is below.

#! /usr/bin/python2.7
import os
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
import datetime
import time
import mysql.connector
def check2(driver, slidebar, sliderknob, percent):
 height = slidebar.size['height']
 width = slidebar.size['width']
 move = ActionChains(driver);
 # slidebar = driver.find_element_by_xpath("//div[@id='slider']/a")
 if width > height:
 #highly likely a horizontal slider
 print "off set: ", percent * width / 100
 move.click_and_hold(sliderknob).move_by_offset(500, 0).release().perform()
 else:
 #highly likely a vertical slider
 move.click_and_hold(sliderknob).move_by_offset(percent * height / 100, 0).release().perform()
 driver.switch_to_default_content()
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-proxy-server')
os.environ["PATH"] += ":/home/mike/software"
os.environ["PATH"] += ":/usr/local/bin/"
try:
 driver = webdriver.Chrome()
 driver.get("http://99.243.40.11/#/HouseSold")
 els = driver.find_elements_by_xpath('//input[@class="input high"]')
 print 'els.len = ', len(els)
 e = els[0]
 ens = driver.find_elements_by_xpath('//span[@class="pointer high"]')
 en = ens[0]
 check2(driver, e, en, 70)
 time.sleep(20)
finally:
 driver.close()

Unfortunately not working for me. Please let me know if you know of any clue. Much appreicate your help.

Regards,

asked Nov 8, 2016 at 10:59
3
  • You just made copy/paste of provided solution... Are you sure you have totally the same HTML for your slider? Provide HTML code of your slider element Commented Nov 8, 2016 at 11:03
  • I changed my code and updated it thanks to your question. So now it finds the input range element to get the width/range from and then looks for its knob which is a span element and then tries to move the knob relatively. For one thing, the example I got inspired from is used for JQuery based sliders but mine is an HTML tag where the actual html tag is a <input type="raghe" > and knob is a span tag. But overall the same idea should work for both cases a it only deals with mouse actions. I think I am missing a point on how to select the knob element. Commented Nov 8, 2016 at 11:34
  • Did you try to record the actions in Selenium IDE, and then viewing the exported code? Commented Nov 8, 2016 at 18:40

1 Answer 1

9

Well I think you can follow last comment's and it will give you the clue.

Actually I did and got some good results. First you need use Selenium IDE to find the knob you like to move and then do sth like below to move it like below.

Let me know if that helps you.

Cheers,

try:
 driver = webdriver.Chrome()
 driver.get("http://99.243.40.11/#/HouseSold")
 en = driver.find_element_by_xpath("//span[6]")
 move = ActionChains(driver)
 move.click_and_hold(en).move_by_offset(10, 0).release().perform()
 time.sleep(5)
 move.click_and_hold(en).move_by_offset(10, 0).release().perform()
 time.sleep(5)
 move.click_and_hold(en).move_by_offset(10, 0).release().perform()
 time.sleep(5)
finally:
 driver.close()
answered Nov 9, 2016 at 7:17
1
  • Thanks it is very helpful. But if I want to move slider to the left move_by_offset(-10,0) is not working. Commented Jul 5, 2020 at 17:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.