Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

python - "No Browser is Open" issue is coming when running the Robot framework script

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...