1

I want to open links that I find on a website in a new tab. I have tried to open a new tab and pass the url of the link to the driver as suggested here, however, the new tab simply will not open. (There are a couple of other suggestion for how to open a new tab, but none of them seem to work for me.)

So my latest attempt was to right-click the link and press "t" to open the link in a new tab, like so:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

# Using Firefox to access web
driver = webdriver.Firefox()

# Open the website
driver.get('https://registers.esma.europa.eu/publication/searchRegister?core=esma_registers_firds')

# search for information
elem = driver.find_element_by_id('keywordField')
elem.clear()
elem.send_keys('XS1114155283')

button = driver.find_element_by_id('searchSolrButton')
button.click()

table_body = driver.find_element_by_xpath("//table[@id='T01']/tbody")
for link in table_body.find_elements_by_tag_name('a'):

    act = ActionChains(driver)
    act.context_click(link)
    act.send_keys("t")
    act.perform()

    # ... do something in the new tab, close tab, and open next link ...

However, I get an error message on act.perform() which reads

MoveTargetOutOfBoundsException: (974, 695) is out of bounds of viewport width (1366) and height (654)

I managed a work around by opening the link in a new window, but I would really prefer the tab-version, as it would take longer to open a new browser window rather than a new tab.

user3820991
  • 2,310
  • 5
  • 23
  • 32
  • Can you please provide a correct `Instrument identification code`, we don't received any details in the search results with `XS1114155283`. – supputuri May 19 '19 at 21:30
  • @supputuri This is a valid ISIN. The website did not seem to work properly a few hours ago. – user3820991 May 19 '19 at 23:00

1 Answers1

2

You can use driver.execute_script() function to open link in a new tab

from selenium import webdriver

# Using Firefox to access web

driver = webdriver.Firefox()

# Open the website
driver.get('https://registers.esma.europa.eu/publication/searchRegister?core=esma_registers_firds')

# search for information
elem = driver.find_element_by_id('keywordField')
elem.clear()
elem.send_keys('XS1114155283')

button = driver.find_element_by_id('searchSolrButton')
button.click()

table_body = driver.find_element_by_xpath("//table[@id='T01']/tbody")
for link in table_body.find_elements_by_tag_name('a'):
    href = link.get_attribute('href')
    # open in new tab
    driver.execute_script("window.open('%s', '_blank')" % href)
    # Switch to new tab
    driver.switch_to.window(driver.window_handles[-1])

    # Continuous your code
supputuri
  • 13,644
  • 2
  • 21
  • 39
Kafels
  • 3,864
  • 1
  • 15
  • 32
  • This works fine, thanks. But it opens a new window, not a new tab. Not that this matters much, but out of curiosity, any idea how to open a new tab instead? also, any idea why the right-click on the link did not work? – user3820991 May 19 '19 at 22:57
  • 1
    I tested this code on Chrome because Firefox was giving an exception to me. Try add the parameter `_blank`: `driver.execute_script("window.open('%s', '_blank')" % href)` and check if works. About the right click, I can't explain why didn't work, unique advise that I say is avoiding as you can of simulate mouse events with selenium, always it is a headache. – Kafels May 19 '19 at 23:42
  • The `_blank` arguemnt does not have an effect. – user3820991 May 20 '19 at 19:34
  • Nevermind, my previous comment. It looks like a discussion on this is a bit futile, see [here](https://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-and-not-a-new-window-using-javascript). – user3820991 May 20 '19 at 20:57