繁体   English   中英

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

[英]How to perform right click using Selenium in python?

我想知道是否有人有解决方案如何对DOM的任何元素执行简单的右键单击操作。 例如,让我们右键单击“ Google搜索”按钮以选择“另存为”选项。 根据我的研究,它涉及ActionChains。 我的代码大致如下:

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)

我在哪里弄错了? ps我的测试环境是Ubuntu 12.04,以防万一。

Google搜索按钮的id属性是gbqfba ,而不是qbqfba (如果您看到的是我正在看到的同一Google搜索页):

btn = br.find_element_by_id('gbqfba')
#                            ^

Google搜索按钮

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM