0

I need to check that an opened tab is empty and switch to another one.

I tried the get_current_url() method, but it does not work.

def check_is_tab_empty(self, link):
    self.click(link)
    self.focus_active_tab()
    tab = self.get_current_url()

This line tab = self.get_current_url() - does not work if a tab is empty, like about:blank.

BlueSheepToken
  • 5,751
  • 3
  • 17
  • 42
Sveta Iva
  • 1
  • 1
  • 2

2 Answers2

1

You have switch to the new tab to get the url of the new tab.Lets take an example here.You have open a blank page.

driver = webdriver.Chrome()
driver.get('https://www.yahoo.com')
windows_before  = driver.current_window_handle
driver.execute_script('''window.open('{}');'''.format("about:blank"))
windows_after = driver.window_handles
new_window = [x for x in windows_after if x != windows_before][0]
driver.switch_to.window(new_window)
print(driver.current_url)
KunduK
  • 32,888
  • 5
  • 17
  • 41
0

I decided to handle this case by the try ... except statement:

        try:
            tab = self.get_current_url()
        except TimeoutException:
            ...
            self.click(link)
            self.focus_active_tab()
            tab = self.get_current_url()

Now it works for me as for the second time a link (not empty one) is opened.

But if someone knows a better solution, please, share.

Sveta Iva
  • 1
  • 1
  • 2