如何在python中使用Selenium执行右键单击?

如何在python中使用Selenium执行右键单击?,python,python-2.7,selenium,selenium-webdriver,right-click,Python,Python 2.7,Selenium,Selenium Webdriver,Right Click,我想知道是否有人能解决如何在DOM的任何元素上执行简单的右键单击操作的问题。例如,让我们右击“谷歌搜索”按钮,选择“另存页面为”选项。 根据我的研究,这涉及到行动链。我的代码大致如下: from selenium import webdriver from selenium.webdriver.support.ui import Select from selenium.webdriver import ActionChains br = webdriver.Firefox() br.get(

我想知道是否有人能解决如何在DOM的任何元素上执行简单的右键单击操作的问题。例如,让我们右击“谷歌搜索”按钮,选择“另存页面为”选项。 根据我的研究,这涉及到行动链。我的代码大致如下:

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver import ActionChains

br = webdriver.Firefox()
br.get('http://www.google.com')
btn=br.find_element_by_id('qbqfba')
actionChains = ActionChains(br)

actionChains.context_click(btn).perform()
出现以下错误:

  File "/usr/local/lib/python2.7/dist-packages/selenium-2.39.0-py2.7.egg/seleniu
m/webdriver/remote/errorhandler.py", line 164, in check_response
    raise exception_class(message, screen, stacktrace)
MoveTargetOutOfBoundsException: Message: u'Offset within element cannot be scrol
led into view: (50, 14.5): [object XrayWrapper [object HTMLButtonElement]]' ; St
acktrace: 
    at FirefoxDriver.prototype.mouseMove (file:///tmp/tmpuIgKVI/extensions/fxdri
ver@googlecode.com/components/driver_component.js:9176)
    at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpuIgKVI/extens
ions/fxdriver@googlecode.com/components/command_processor.js:10831)
    at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpuIgKVI/extensio
ns/fxdriver@googlecode.com/components/command_processor.js:10836)
    at DelayedCommand.prototype.execute/< (file:///tmp/tmpuIgKVI/extensions/fxdr
iver@googlecode.com/components/command_processor.js:10778)
File”/usr/local/lib/python2.7/dist-packages/selenium-2.39.0-py2.7.egg/seleniu
m/webdriver/remote/errorhandler.py”,第164行,在check_响应中
引发异常类(消息、屏幕、堆栈跟踪)
MoveTargetOutOfBoundsException:消息:元素内的u'Offset不能为scrol
引入视图:(5014.5):[ObjectXrayWrapper[ObjectHtmlButtoneElement]];圣
确认跟踪:
在FirefoxDriver.prototype.mouseMove(file:///tmp/tmpuIgKVI/extensions/fxdri
ver@googlecode.com/components/driver_component.js:9176)
在DelayedCommand.prototype.executeInternal\uh处(file:///tmp/tmpuIgKVI/extens
离子/fxdriver@googlecode.com/组件/命令(处理器js:10831)
在DelayedCommand.prototype.executeInternal\u(file:///tmp/tmpuIgKVI/extensio
ns/fxdriver@googlecode.com/组件/命令(处理器js:10836)
在DelayedCommand.prototype.execute/<(file:///tmp/tmpuIgKVI/extensions/fxdr
iver@googlecode.com/组件/命令处理器(js:10778)
我哪里弄错了?
p、 我的测试环境是Ubuntu12.04,以防万一。

谷歌搜索按钮的
id
属性是
gbqfba
,而不是
qbqfba
(如果你看到的是我看到的同一个谷歌搜索页面):

或者,您可以通过文本查找按钮:

br.find_element_by_xpath('.//button[contains(., "Google Search")]')

谢谢你。它工作得很好。现在是进行另一个步骤的时候了,如何控制右键单击以执行“另存为页面”?@karolinastamblewska,按
p
(将页面另存为的快捷键)
actionChains。发送键('p')。执行()
,现在将弹出一个保存。。对话框。@karolinastamblewska,顺便说一句,您可以使用
br.page\u source
访问页面的源代码。如果您只想保存页面的源代码,只需将
br.page\u source
保存到一个文件中即可。非常感谢,这将再次推进我的项目,这远远超出谷歌搜索,但废弃方法仍然类似;)
br.find_element_by_xpath('.//button[contains(., "Google Search")]')