4

There is a link on my tested page which is opened in new TAB by default. I need to open the link and verify some values on the newly opened page. As I found selenium does not support working with tabs, so I am trying to open the link in new window, but it still does not work.. I implemented python function to hold SHIFT key (I have done this before for CTRL and it works) and then I called "click" function, but the link is still being opened in new tab

from robot.libraries.BuiltIn import BuiltIn
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

class CustomSeleniumLibrary(object):
    def __init__(self):
        self.driver = None
        self.library = None
        self.ac = None

    def get_library_instance(self):
        if self.library is None:
            self.library = BuiltIn().get_library_instance('ExtendedSelenium2Library')
        return self.library

    def get_action_chain(self):
        if self.ac is None:
            self.ac = ActionChains(self.get_library_instance()._current_browser())
        return self.ac

def hold_shift(self):
        actionChain = self.get_action_chain()
        actionChain.key_down(Keys.SHIFT)
        actionChain.perform()

The robot keyword is

Open project detail
     wait until element is visible  ${LINK_TO_PROJECT}
     ${project}=  get text  ${LINK_TO_PROJECT}
     hold shift
     click element   ${LINK_TO_PROJECT}
     #sleep  2s
     #release shift
     element should contain   //h3  Project Details: ${project}

I tried many variants with sleeps, releasing the key etc. but it never really opens the link in new window. I also tried to verify the data in newly opened tab (without trying to open in new window), but it is always redirected into original tab very quickly so the DOM on new tab is not loaded yet.. Thanks for any suggestion!

neliCZka
  • 945
  • 1
  • 16
  • 27
  • Why you think that `selenium` doesn't support working with tabs? – Andersson Mar 20 '17 at 16:17
  • 1
    http://stackoverflow.com/questions/37088589/selenium-wont-open-a-new-url-in-a-new-tab-python-chrome – dot.Py Mar 20 '17 at 16:19
  • Well I found it in several threads...also I found some solution where user passes the link manually in the code, but I don't know what is the address in the link..but it could be probably taken as value of href and then pass it as variable...will check – neliCZka Mar 20 '17 at 16:29

1 Answers1

1

You can use below code to handle page opened in new tab:

current = driver.current_window_handle
driver.find_element_by_css_selector('a').click() # use own selector
new_tab = [tab for tab in driver.window_handles if tab != current][0]
driver.switch_to.window(new_tab)
# do some actions
driver.close()
driver.switch_to.window(current)

Also you can make little hack (not recommended, but...) to avoid handling new tabs and force link to open in the current tab:

link = driver.find_element_by_css_selector('a')
driver.execute_script('arguments[0].target="_self";', link)
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • Thanks, the solution works nice :) Just now I need also to open new empty tab (not click on any link, but just open it for copy paste action), so I called function containing this self.get_action_chain().key_down(Keys.CONTROL).key_down('t').perform() from robot and nothing happens... :( I tried many variants of this, with send_keys, combination of key_down and send_keys and still no tab is opened...any idea please? – neliCZka Mar 22 '17 at 14:39
  • 1
    With `selenium` you can open new tab like `driver.execute_script("window.open('http://google.com')")`. If not to pass `URL` as argument, just empty tab will be opened – Andersson Mar 22 '17 at 14:49
  • Hi, please, do you know how to close the new TAB? I tried close() when I was switched in the new tab, but it closed whole window...I cannot find any working solution :( – neliCZka Jun 07 '17 at 12:19
  • Hm... `driver.close()` should do this. If it doesn't work you also might try `driver.exexcute_script('window.close();')` – Andersson Jun 07 '17 at 12:37
  • In both cases it shuts down whole window, not just the tab :/ I have some other workaround in the test for now, but it is weird...thank you so much anyway! :) – neliCZka Jun 07 '17 at 12:45