3

I want to use selenium webdriver methods in the robot framework library.

def custom_go_to
        driver = BuiltIn().get_library_instance('SeleniumLibrary')
        driver.go_to(url)

The above code from custom library works fine, but I want to use selenium method at the place of robotframework builtin library. When I try to use driver.get(url) it says

'SeleniumLibrary' object has no attribute 'get'

The custom library I created ERP.py looks like

class ERP: 
   @keyword
    def custom_go_to(self, url):
        driver = BuiltIn().get_library_instance('SeleniumLibrary')
        driver.get(url)

And Test Case looks like

***Settings***
Library  SeleniumLibrary
Library  path_to_lib/ERP.py

*** Variable ***
${BROWSER}  |  chrome
${URL}  |  facebook.com

***Test Cases***
Open the browser using an inbuilt keyword and go to a given URL using custom go to using EventFiringWebDriver.
     Open Browser |  about:blank  |  ${BROWSER}
     Custom Go To  |   ${URL}

How can I use Selenium webdriver methods inside the robot framework library?

5
  • Don't know what you are trying to achieve here, but the newest SeleniumLibrary does not have Get keyword... Please try again with an existing keyword. Commented Oct 22, 2019 at 10:45
  • I want to use all Selenium keywords inside the robotframework library. Like we do normally when we use Selenium with Python only. Commented Oct 22, 2019 at 11:09
  • Still, there is no Get keyword in SeleniumLibrary... Commented Oct 22, 2019 at 11:21
  • Thank you for pointing me out, I just updated the question. I want to use webdriver methods. :) Commented Oct 22, 2019 at 11:32
  • There's a quite good tutorial for Python and webdriver here: selenium-python.readthedocs.io/… Commented Oct 22, 2019 at 11:46

4 Answers 4

6

The selenium library itself is not a webdriver object, it's just an instance of the SeleniumLibrary class. You need to get a reference to the driver, which is an attribute in the library.

def custom_go_to(url):
    selib = BuiltIn().get_library_instance('SeleniumLibrary')
    selib.driver.get(url)

For more information about interacting with SeleniumLibrary at a low level, see the document Extending SeleniumLibrary in the SeleniumLibrary git repository.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Bryan, this is exactly what I wanted to know. I've achieved the same by creating the plugin with the help of LibraryComponent. In plugin I'm using it as self.driver.get(url) and it works but self doesn't work with the library, why?
@DivakshJain: I don't know what you mean by "self doesn't work with the library". self works the same way in all python code. There's nothing unique about robot keyword classes or selenium that would make it work differently.
I got it now. Thank you once again. :)
0

'SeleniumLibrary' do not have get attribute. Please refer to Selenium Library Robot Framework for more info.

You should be using

Open Browser    ${LOGIN URL}    ${BROWSER}

Below is example from the link
Example

*** Settings ***
Documentation     Simple example using SeleniumLibrary.
Library           SeleniumLibrary

*** Variables ***
${LOGIN URL}      http://localhost:7272
${BROWSER}        Chrome

*** Test Cases ***
Valid Login
    Open Browser To Login Page


*** Keywords ***
Open Browser To Login Page
    Open Browser    ${LOGIN URL}    ${BROWSER}

1 Comment

This doesn't answer the question that was asked. There are good reasons for using a custom keyword rather that using the pre-built keywords.
0

SeleniumLibrary have a "Get Element Attribute" keyword.

See documentation here: RobotFramework SeleniumLibrary Documentation

Comments

-1

Reproduced this as well:

driver.get(url) produces AttributeError: 'SeleniumLibrary' object has no attribute 'get'

An error with an existing keyword, such as driver.go_to(url) produces a different error: No browser is open.

So, use existing keywords or make your own.

7 Comments

"So, use existing keywords or make your own." - that's exactly what they are trying to do: make their own.
yes, exactly in the case of driver.get(). They are wanting to create a custom keyword that calls driver.get().
In this case, driver.get() does not exist, so, it must be created or then change to another keyword than get(), thus, "So, use existing keywords or make your own". You can create a million custom keywords, but that doesn't make that driver.get() working.
You are misreading the question. the get method is a standard method of a browser driver. That is what they want to call. In the question they are incorrectly assuming that the instance of SeleniumLibrary is a driver. It is not.
They changed some of the wording, but even the original version of the question said they wanted to call the low level get method of the selenium driver. That method exists, but it exists on the driver, not on the instance of SeleniumLibrary.
|

Your Answer

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.