How to combine two ready selenium2library keywords?

770 views
Skip to first unread message

Huy Chau

unread,
Jan 8, 2016, 7:59:35 AM1/8/16
to robotframework-users
Hello guys,

I am currently trying to select multiple rows in a table cell but have not found any way to complete the Shift holding key. I am wondering if I could combine 2 ready keywords from selenium2library "Press Key" and "Click Element" to fulfill my need?

If it is possible, how can it be done?

Much appreciate,
Huy

Tatu Aalto

unread,
Jan 11, 2016, 1:33:29 AM1/11/16
to cha...@gmail.com, robotframework-users

Ugh

What you ask is possible, by extending Selenium2Library, but before doing that have you tried the Select From List keyword. Or the Select From List By* keywords?

-Tatu
Send from my mobile
[1] http://robotframework.org/Selenium2Library/doc/Selenium2Library.html#Select%20From%20List

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.
To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

Huy Chau

unread,
Jan 11, 2016, 2:41:48 AM1/11/16
to robotframework-users, cha...@gmail.com
Hello Tatu,

Thanks for the suggestion. I have tried but still can't find a way to highlight multiple rows in a table cell with "Select From List" or "Select From List By*" keywords.
How does it include the "Shift" key in this case? 

For example: my table cell includes 10 rows (.ui-grid-row), I would like to left-click on second row and hold shift then left-click 6th row. 

I can definitely use "Click Element" but the middle-progress with "Shift" key is still a misery. 

Kiitos paljon,

Tatu Aalto

unread,
Jan 11, 2016, 2:19:32 PM1/11/16
to cha...@gmail.com, robotframework-users
Ugh

Unfortunately there is not keyword which could Shift + Click out of the box, but the good news is that it is easy to extend Selenium2Library (or any library in Robot Framework) and make a keyword that does the trick what you need. To be able press key down, while you click, you must use selenium ActionChains [1] and you need to get the Selenium2Library instance [2] from Robot Framework during run time. Something like this could work:

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from robot.libraries.BuiltIn import BuiltIn


def press_key_with_shift(*selectors):
    if not selectors:
        raise ValueError()
    s2l = BuiltIn().get_library_instance('Selenium2Library')
    driver = s2l._current_browser()
    actions = ActionChains(driver)
    actions.key_down(Keys.SHIFT)
    for selector in selectors:
        elem = driver.find_element_by_name(selector)
        actions.click(elem)
    actions.key_up(Keys.SHIFT).perform()


-Tatu
[1] http://selenium.googlecode.com/svn/trunk/docs/api/py/webdriver/selenium.webdriver.common.action_chains.html#module-selenium.webdriver.common.action_chains
[2] http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#getting-active-library-instance-from-robot-framework

Huy Chau

unread,
Jan 13, 2016, 8:17:38 AM1/13/16
to robotframework-users, cha...@gmail.com
Hi Tatu,

Maybe I am a little bit too dumb. I created a python class with your "press_key_with_shift" function below and tried to use it into my RF test case as:

Press Key With Shift    css=.ui-grid-row:nth-child(2)   css=.ui-grid-row:nth-child(6)


I receive error while running: "NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"css=.marketInfo .ui-grid-pinned-container-left .ui-grid-viewport .ui-grid-canvas .ui-grid-row:nth-child(2) .ui-grid-cell"}
  (Session info: chrome=47.0.2526.106)
  (Driver info: chromedriver=2.19.346078 (6f1f0cde889532d48ce8242342d0b84f94b114a1),platform=Windows NT 6.1 SP1 x86_64)"

Is there anything I am missing?

Regards,
Huy
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.

m m

unread,
Jan 13, 2016, 4:41:50 PM1/13/16
to cha...@gmail.com, robotframework-users
Ugh

I did hard code how the element is searched and I didn't use parsing provided by the Selenium2Library. If you look at the line 14 in your class, there is: find_element_by_name, if you want to use  CSS as selector, change: find_element_by_name to find_element_by_css and provide the locator without the css= part.

-Tatu

From: Huy Chau
Sent: ‎13/‎01/‎2016 15:17
To: robotframework-users
Cc: cha...@gmail.com
Subject: Re: How to combine two ready selenium2library keywords?

To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.

Huy Chau

unread,
Jan 14, 2016, 6:15:27 AM1/14/16
to robotframework-users, cha...@gmail.com
Super! 

It works like a champ now. Except I had to modify the "driver.find_element_by_css" to "driver.find_element_by_css_selector" 

Thanks a lot, Tatu :)
Reply all
Reply to author
Forward
0 new messages