-1

I have created Test.py file which has some function in it and using those function names as Keywords in sample.robot file.

Ex: - Test.py

def Launch_URL(url):
    driver.get(url)

def article(publication):   
    do something here

Sample.robot

Library  Selenium2Library
Library  Test.py

*** Test Cases ****
 Open app     
     Launch URL   "https://stackoverflow.com"
     article       something
     Click Element  xpath=/html/body/div[1]/div/div/button/i

Apart from the derived keywords in .py I also want to use built in Keyword Click Element in robot file. When the I run the above script it is throwing me No Browser Open error for Click Element keyword.

halfer
  • 19,824
  • 17
  • 99
  • 186
Uday
  • 121
  • 1
  • 4
  • 15
  • I don't see anywhere where you are opening a browser. They don't just appear out of nowhere, you have to explicitly open one. – Bryan Oakley Dec 14 '17 at 13:29
  • I have not mentioned in the comment but I have used driver =webdriver.Chrome() in my .py file – Uday Dec 14 '17 at 13:33
  • And are you sure that `driver =webdriver.Chrome()` is actually executed without error prior to calling `Launch_URL`? – Würgspaß Dec 14 '17 at 13:39
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Dec 14 '17 at 13:47
  • Yes it is working – Uday Dec 14 '17 at 13:58

1 Answers1

1

The click element keyword (and all of the other keywords) relies on a browser opened by Selenium2Library. Since you are opening it with the python selenium module rather than the robot library, the Selenium2Library keywords do not know about the browser.

If you need to use the same browser both for Selenium2Library and through python code, The best way is to open the browser with Selenium2Library, and then get the driver reference from it to use it in python.

Assuming that you've opened the browser using the open browser or create webdriver keyword, you can use the driver for that browser in python like this:

from robot.libraries.BuiltIn import BuiltIn
def Launch_URL(url):
    se2lib = BuiltIn().get_library_instance('Selenium2Library')
    driver = se2lib._current_browser()
    driver.get(url)

If you do not want to use open browser in your test, and expect Launch URL to be the first keyword you use, you can call open_browser from your keyword:

def Launch_URL(url):
    se2lib = BuiltIn().get_library_instance('Selenium2Library')
    se2lib.open_browser(url, "chrome")

Here's another similar question: Pass existing Webdriver object to custom Python library for Robot Framework

If you are wanting to write keywords in python that use both the built-in keywords and also have directly access to the selenium module, you might want to consider using this page object library, which handles all of the details for you.

Note: the use of the private method _current_browser is unavoidable in version 2 of Selenium2Library. A public interface is being made available in version 3 of SeleniumLibrary. See github issue #882

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • I have already declared driver = webdriver.Chrome() outside the launch_url function and where to add the above code. If I have done the same , browser is not opening at all.Can you please help me on this? – Uday Dec 14 '17 at 14:47
  • 1
    @Uday: if you use `driver = webdriver.Chrome()`, the Selenium2Library keywords won't work. It's as simple as that. You need to use the `open browser` keyword before calling any other selenium functions. – Bryan Oakley Dec 14 '17 at 15:05