1

I want to click on an element(found by xpath) but if i use:

ActionChains(driver).key_down(Keys.CONTROL).move_to_element(myElement).click().key_up(Keys.CONTROL).perform()

-> I get a new tab but the current tab also Redirects to the page

So what I need is a way to perform a right click and choose "open in new tab" but that does not work for me.

I use this code but after I get into the context_menu nothing happens.

ActionChains(driver).context_click(myElement).send_keys(Keys.DOWN).send_keys(Keys.RETURN).perform()

Another way would be to click "myElement" with the scroll wheel button but i can not find the click method for that.

Thank you.

(I do not need anything with Control + mouse click :-) )

sucherfrau
  • 11
  • 1
  • Possible duplicate of [Opening a new tab using Ctrl + click combination in Selenium Webdriver](https://stackoverflow.com/questions/46201035/opening-a-new-tab-using-ctrl-click-combination-in-selenium-webdriver) – undetected Selenium Mar 22 '18 at 14:18

1 Answers1

0

If the trouble is managing the window/tab focus, you could use window_handles as in this post ? I didn't understand if you wanted to get control over previous tab or the next one. For the first tab, that would be something like

driver.switch_to_window(driver.window_handles[1])

Edit : try this (3 steps, requiring first to acquire the target, then opening a blank new tab, then reaching target)

from selenium.webdriver.common.keys import Keys
href = myElement.get_attribute("href")
current_page = driver.find_element_by_tag_name('body')
current_page.send_keys(Keys.CONTROL + 't') #open new tab
driver.switch_to_window(driver.window_handles[1])
driver.get(href)
tgrandje
  • 2,332
  • 11
  • 33
  • sorry, I didn't understand your "redirect" mention – tgrandje Mar 22 '18 at 13:10
  • did you try to gather the element's href, then open a new tab and setting the url manually ? – tgrandje Mar 22 '18 at 13:12
  • so, ist just Driver.get(href)... but the part with opening new clean tab does not work, so currentpage... and current_page.send... do not work. I get: Error while creating file: Message: Browsing context has been discarded – sucherfrau Mar 22 '18 at 13:33
  • I can't reproduce this error though I have been using this code on two different versions of python & firefox. Do you grab the current_page correctly ? – tgrandje Mar 22 '18 at 13:39
  • I have alreary: from selenium.webdriver.common.keys import Keys I also grab the current page correctly if 'body' is truly the tag name. I use the latest Version of selenium, python 3.5.4 and Firefox 59 – sucherfrau Mar 22 '18 at 13:40
  • Do you have a body in the html source code ? What does current_page returns ? (using firefox 45.0.1 & selenium 3.8.0) – tgrandje Mar 22 '18 at 13:43