无法在python中创建Web驱动程序实例

时间:2015-07-28 11:10:33

标签: python selenium robotframework

我正在尝试使用以下代码在python中创建一个Web驱动程序实例:

from robot.libraries.BuiltIn import BuiltIn
import Selenium2Library
from Selenium2Library import Selenium2Library

def get_webdriver_instance():
    s2l = BuiltIn().get_library_instance("Selenium2Library")
    return s2l._current_browser()

但在BuiltIn()。get_library_instance(" Selenium2Library")我收到以下错误:

    Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        File "/usr/lib/python2.6/site-packages/robot/libraries/BuiltIn.py", line 2922, in get_library_instance
        return self._namespace.get_library_instance(name)
        File "/usr/lib/python2.6/site-packages/robot/libraries/BuiltIn.py", line 70, in _namespace
        return self._context.namespace
        File "/usr/lib/python2.6/site-packages/robot/libraries/BuiltIn.py", line 65, in _context
        raise RobotNotRunningError('Cannot access execution context')
robot.libraries.BuiltIn.RobotNotRunningError: Cannot access execution context

有人可以帮我解决此错误

2 个答案:

答案 0 :(得分:1)

错误消息告诉您,除非您实际运行测试(通过pybot,jybot等),否则您无法使用BuiltIn库的方法。您无法在独立的python脚本中调用BuiltIn().get_library_instance('Selenium2Library')

答案 1 :(得分:0)

_current_browser仅返回当前浏览器

如果你想在python中使用Selenium2Library,那么你可以使用下一个

from Selenium2Library import Selenium2Library
sl = Selenium2Library()
sl.open_browser('firefox')