“没有打开浏览器"运行机器人框架脚本时出现问题 [英] "No Browser is Open" issue is coming when running the Robot framework script

查看:79
本文介绍了“没有打开浏览器"运行机器人框架脚本时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了 Test.py 文件,其中包含一些函数,并在 sample.robot 文件中使用这些函数名称作为关键字.

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

例如:- 测试.py

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

def article(publication):   
    do something here

Sample.robot

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

除了 .py 中的派生关键字之外,我还想在机器人文件中使用内置关键字 Click Element.当我运行上面的脚本时,它会为 Click Element 关键字抛出 No Browser Open 错误.

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.

推荐答案

点击元素 关键字(以及所有其他关键字)依赖于由 Selenium2Library 打开的浏览器.由于您是使用 python selenium 模块而不是机器人库打开它,因此 Selenium2Library 关键字不知道浏览器.

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.

如果您需要为 Selenium2Library 和通过 python 代码使用相同的浏览器,最好的方法是使用 Selenium2Library 打开浏览器,然后从中获取驱动程序引用以在 python 中使用它.

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.

假设您已使用打开浏览器打开浏览器或create webdriver 关键字,您可以像这样在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)

如果您不想在测试中使用 open browser,并且希望 Launch URL 成为您使用的第一个关键字,您可以调用 open_browser 来自您的关键字:

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")

这是另一个类似的问题:将现有的 Webdriver 对象传递给 Robot Framework 的自定义 Python 库

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

如果您想在 python 中编写既使用内置关键字又可以直接访问 selenium 模块的关键字,您可能需要考虑使用这个 页面对象库,它为您处理所有细节.

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.

注意:在 Selenium2Library 版本 2 中不可避免地使用私有方法 _current_browser.SeleniumLibrary 的第 3 版中提供了一个公共接口.见 github 问题 #882

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

这篇关于“没有打开浏览器"运行机器人框架脚本时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆