I encountered the same issue where I had to right-click and click on 'open link in new tab'.
I searched for a lot of answers on google but there was no specific solution I found for Python.
Earlier, I was doing using ActionChains
where right-click menu is showing, but then that menu list can't be accessed in selenium as I found some threads saying this has OS-level access.
action = ActionChains(driver)
action.context_click(<obj_which_u_want_to_click>).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ENTER).perform()
Here, Keys.ARROW_DOWN
is not working, and opening the link in the same tab, ideally, it should open in a new tab.
So, there are two ways through which I did this:
First, via send_keys
:
link = driver.find_elements_by_xpath("//a[contains(@href, 'https:...')]")
link.send_keys(Keys.CONTROL + Keys.ENTER)
Second, through JavaScript:
driver.execute_script("window.open(arguments[0], '_blank');", link)
I think you can't access the right-click menu items in selenium as it is out of its scope.