2

I wonder if someone has solution how to perform trivial right click action on any element of DOM. Let's for example do right click on the 'Google Search' button to select 'Save Page As' option. According to my research it involves ActionChains. Roughly my code is as follows:

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()

The following errors occurs:

  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
[email protected]/components/driver_component.js:9176)
    at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpuIgKVI/extens
ions/[email protected]/components/command_processor.js:10831)
    at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpuIgKVI/extensio
ns/[email protected]/components/command_processor.js:10836)
    at DelayedCommand.prototype.execute/< (file:///tmp/tmpuIgKVI/extensions/fxdr
[email protected]/components/command_processor.js:10778)

Where I get wrong with it? p.s. My testing environment is Ubuntu 12.04, just in case is matter.

1 Answer 1

2

The id attribute of the Google Search button is gbqfba, not qbqfba (if you are seeing the same google search page I am seeeing):

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

Google Search button

Alternatively you can find button by text:

br.find_element_by_xpath('.//button[contains(., "Google Search")]')
Sign up to request clarification or add additional context in comments.

4 Comments

thanks falsetru. It works perfect. Now is time for another step, how to control right click to perform "save as page"?
@karolinastamblewska, Press p (shortcut key for Save Page as) actionChains.send_keys('p').perform(), now it will pop up a save .. dialog.
@karolinastamblewska, BTW, you can use br.page_source to access page's source. If you want to only save the source code of the page, simply save br.page_source to a file.
Many thanks, once again it will be advance my project, which is far beyond google search, however the scrapping method is still similar ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.