0

I try to open the right button menu from web then select one of the option(e.g. print... )? I'm using selenium and python.Does anyone know how to do this? Thanks a lot!

Vao Tsun
  • 47,234
  • 13
  • 100
  • 132
sansha
  • 1
  • 3
  • 6
  • Cannnot understand why the following code doesn't work on Chrome+Python+Selenium. The thing what I want to do is open the right button menu on chrome webpage then select the print... from right button menu. I saw the right button menu was opened, but I didn't see it can be moved to printing then click it... Anyone can help... img = driver.find_element_by_tag_name('img') ActionChains(driver).context_click(img).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).perform() – sansha Sep 18 '14 at 21:58

1 Answers1

0

I don't know if I understood you well.

For example try this:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("https://www.google.pl")
driver.find_element_by_xpath(".//*[@id='gbwa']/div[1]/a").click()
driver.find_element_by_xpath(".//*[@id='gb36']/span[1]").click()
element = driver.find_element_by_xpath("//body")
element.send_keys(Keys.CONTROL, 'p')
print driver.current_url
driver.close()

And read about xpath

CodeNinja
  • 1,168
  • 2
  • 14
  • 27
  • Hi CodeNinja, Thanks a lot for your answer. Actually, I need to open a image on chrome browser from a link. Then I want to print it. It seems there is no way to perform ctrl+p for chrome with python+selenium, so I want to access the print... from the right button menu on this image webpage. Do you have any suggestions on this? Thanks! – sansha Sep 18 '14 at 20:17
  • I changed my answer. Ctrl+P Works on Firefox. – CodeNinja Sep 18 '14 at 20:28
  • yes, i know, but the problem is it doesn't work on Chrome, so I'm trying to find a way to do the same thing on Chrome. Thanks! – sansha Sep 18 '14 at 20:29
  • Then maybe try something like this: `ActionChains(driver).context_click(element).perform()` – CodeNinja Sep 18 '14 at 21:07
  • Yes I can open the right button menu by the code: img = driver.find_element_by_tag_name('img') ActionChains(driver).context_click(img).perform(), but I still don't know how to select one of the item from this menu. Is there any method can be used to finish this? Thanks for your help! – sansha Sep 18 '14 at 21:10
  • Yes, it doesn't work... I tried this code ActionChains(driver).context_click(img).send_keys(u'\ue015').send_keys(Keys.RETURN).perform(), but it doesn't work, the menu can be opened, but I cannot select any item. – sansha Sep 18 '14 at 21:41
  • I think selenium not support that actions. Maybe try something like this: [link1](http://stackoverflow.com/questions/2575528/simulating-key-press-event-using-python-for-linux/2576118#2576118) [link2](http://stackoverflow.com/questions/5714072/simulate-keystroke-in-linux-with-python) – CodeNinja Sep 18 '14 at 21:48
  • Sorry, I'm the new for selenium. Do you know if the right button click menu from chrome webpage has the link name? BTW, what's the 'gbwa' in your script? – sansha Sep 18 '14 at 21:52
  • 'gbwa' is id value of the item on the page, that I want to click. [Try Firegbug to inspect element on google site](https://getfirebug.com/html) – CodeNinja Sep 18 '14 at 21:58