2

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.

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

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

4 Comments

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?
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.
The _blank arguemnt does not have an effect.
Nevermind, my previous comment. It looks like a discussion on this is a bit futile, see here.

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.