0

I'm writing a bot with Python/Selenium.

In my process, I want :

  • to right click on a picture
  • open it in a new chrome tab

I tried the following :

def OpenInNewTab(self):
        content = self.browser.find_element_by_class_name('ABCD')
        action = ActionChains(self.browser)
        action.move_to_element(content).perform();
        action.context_click().perform()
        action.send_keys(Keys.DOWN).perform()
        action.send_keys(Keys.ENTER).perform()

However, the problem is that my bot :

  • open the contextual menu
  • scroll down on the page and not on the contextual menu

After a lot of researches, I tried with :

import win32com.client as comclt       
wsh = comclt.Dispatch("WScript.Shell")
wsh.SendKeys("{DOWN}")
wsh.SendKeys("{ENTER}")

However, it's not really working.

I saw many other topics, like this one (supposing there is href associated to the pic)

Then, i'm a little lost to be able to do tthis simple thing : open a righ click on contextual an element and select open in a new tab. I'm open to any advice / new road to follow.

3
  • Can you post the link & which browser are using? Commented Apr 2, 2020 at 23:30
  • please share the html of the link. Commented Apr 3, 2020 at 2:36
  • No need for it, my question could be general and i' using chrome/chromedriver Commented Apr 3, 2020 at 7:35

1 Answer 1

1

In my experience it will be difficult to achieve a perfect "one fits all" solution involving the (context menu - new tab) combination, and I tend to keep clear of all the headache it can bring.

My strategy would be a bit different, and, on a case by case basis, I'd use something like:

base_window = driver.current_window_handle  # this goes after you called driver.get(<url here>)

my_element=driver.find_element_by_xpath(...) #or whatever identification method
my_element.send_keys(Keys.CONTROL + Keys.ENTER)

driver.switch_to.window(driver.window_handles[1])  #switch to newly opened tab

driver.switch_to.window(base_window)  # switch back to the initial tab

An alternative workaround is using hrefs - first open a new tab, then load the fetched href(s). Here's an example:

url='https://www.instagram.com/explore/tags/cars/?hl=en'
driver.get(url)
base_window = driver.current_window_handle

a_tags=driver.find_elements_by_xpath("//div[@class='v1Nh3 kIKUG  _bz0w']//a")
hrefs=[a_tag.get_attribute('href') for a_tag in a_tags] #list of hrefs

driver.execute_script("window.open();")   #open new tab
driver.switch_to.window(driver.window_handles[1]) #switch to new tab
driver.get(hrefs[0])  #get first href for example
driver.close()  #close new tab
driver.switch_to.window(base_window)  #back to initial tab
Sign up to request clarification or add additional context in comments.

2 Comments

Very good method, except that i absolutely need to be able to go on the contextual menu because the website (Instagram, on pics) doesn't let CTRL+ ENTER working on pics
Added another approach using `href' urls. Again staying away from the context menu, but the end result should be the same.

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.