我想知道是否有人有解决方案如何对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)
我哪里出错了? 附:我的测试环境是Ubuntu 12.04,以防万一。
答案 0 :(得分:1)
Google搜索按钮的id
属性为gbqfba
,而不是qbqfba
(如果您看到我正在看到的相同Google搜索页面):
btn = br.find_element_by_id('gbqfba')
# ^
或者,您可以按文字找到按钮:
br.find_element_by_xpath('.//button[contains(., "Google Search")]')