您的位置:首页 > 编程语言 > Python开发

RF+Python实现右键操作

2016-04-15 13:21 615 查看
首先感谢虫师分享的文章,给了我解决问题的思路。我的问题:在RF中实现右键操作,但是S2L里面 貌似没有现成的关键字供RF调用。由于机缘巧合,找到了虫师的文章:http://www.cnblogs.com/fnng/p/3288444.html,用selenium实现了右键菜单。现在要让RF可以调用,需要稍微改造一下。

不废话了,直接上代码:

在自定义lib文件中增加下面的函数:right_click_element(xpath, window)

from selenium import webdriver

from selenium.webdriver.common.action_chains import ActionChains

import time

def right_click_element(xpath, window):

driver = window

time.sleep(2)

elem = driver.find_element_by_xpath(xpath)

ActionChains(driver).context_click(elem).perform()

time.sleep(3)

现在问题的关键是,window这个参数,就是当前浏览器对象,如何获取呢?

--修改一下 ..\site-packages\robotframework_selenium2library-1.7.dev-py2.7.egg\Selenium2Library\keywords\_browsermanagement.py文件,增加一个返回当前浏览器对象的函数:

def get_current_window(self):

"""

@ Edited by solon-20160405

@ Selects the window found with `locator` as the context of actions.

"""

return self._current_browser()

重启一下robot,调用右键函数,可以显示右键菜单了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: