Need help in sliders on a page with selenium

947 views
Skip to first unread message

Shailesh Kumar

unread,
Oct 29, 2016, 4:57:38 PM10/29/16
to robotframework-users
I need help in a keyword of selenium library... We have Get Element Attribute do we have Set Element Attribute? What I am doing is that we have a two ¤ on a slider one is lower and one is upper <div class="slider-lower" style="left: 0%;"> and the upper one is <div class="slider-upper" style="left: 100%;">. Now I can get the style attributes with Get Element Attributes but I want selenium to drag lower slider ¤ to 20% and upper slider ¤ to 80% like I can do it manually and the attributes look like <div class="slider-lower" style="left: 20%;"> and the upper one <div class="slider-upper" style="left: 80%;">. How to do that?

Paolo De Grazia

unread,
Oct 30, 2016, 9:39:29 AM10/30/16
to robotframework-users
To set attributes, you can execute some Javascript:

document.getElementById("yourid").setAttribute("yourattribute""yourvalue");

You can use it like this in RF with the Selenium2library:
Execute Javascript | document.getElementById("yourid").setAttribute("yourattribute","yourvalue");

Some people even managed to inject javascript to make the element scroll (it never worked for me, but I have always tried in an iframe...)

Execute Javascript | window.scrollBY(x,y);


Another thing you can do is focusing on the slider, then simulate as many keypresses of the down arrow as you need, but this might not be accurate or might require a little bit of tweaking

I moved to logic to python:
def scroll_element(self, container_locator):
start_time = time.time()
content =
Selenium2Library.get_webelement(container_locator)

while time.time() < start_time + timeout:
          content.send_keys(Keys.ARROW_DOWN)
if content.get_attribute("yourattribute") == '20%':
return
    #this is to make sure your kw is failing in case the value is not set to 20%, helpful for debugging. If removed, your test might fail later on, making debugging harder
raise Exception('Value was not set to blablah')

On Saturday, October 29, 2016 at 9:57:38 PM UTC+1, Shailesh Kumar wrote:


Enjoy testing!

Paolo

Shailesh Kumar

unread,
Nov 1, 2016, 11:34:12 AM11/1/16
to robotframework-users
I tried with the following:
To set attributes, you can execute some Javascript:
document.getElementById("yourid").setAttribute("yourattribute""yourvalue");
You can use it like this in RF with the Selenium2library:
Execute Javascript | document.getElementById("yourid").setAttribute("yourattribute","yourvalue");

Resust:

Executing JavaScript:
document.getElementById("xpath=.//*[@class="slider-lower"]").setAttribute("style","left: 20%;");
WebDriverException: Message: unknown error: Runtime.evaluate threw exception: SyntaxError: missing ) 

How do I run the Python Logic in the keyword?

Sorry I am new to the scripting.

Kevin O.

unread,
Nov 2, 2016, 11:53:02 AM11/2/16
to robotframework-users
There is no set element attribute keyword in Selenium2Library. There is also also no set_attribute method in the WebElement interface in Selenium. Javascript might be the way to go. 

You are getting the error because you are using a locator where the HTML ID should be.
Since you do not have an HTML ID, you can assign one. Then use it in your script:

Assign Id To Element    xpath=.//*[@class="slider-lower"]    s2l-slider-lower
Execute Javascript    document.getElementById("s2l-slider-lower").setAttribute("yourattribute","yourvalue");

Shailesh Kumar

unread,
Nov 3, 2016, 11:46:14 AM11/3/16
to robotframework-users
That worked!!

Thanks...

Shailesh
Reply all
Reply to author
Forward
0 new messages