def close_tab(self, browser_instance=None, tab_number=None, browser_type="firefox"):
"""Closing tabs in a browser with unique tab_number"""
if browser_instance is None:
browser_instance = self.current_browser
if len(browser_instance.window_handles) > 1:
prior_current_tab = False
current_window_handler = browser_instance.current_window_handle
for i in range(0, len(browser_instance.window_handles)):
if browser_instance.window_handles[i] == current_window_handler:
prior_current_tab = i
status = True
if tab_number is not None:
status = self.switch_tab(browser_instance, tab_number, browser_type)
if status:
tab_number = int(tab_number) - 1
browser_instance.find_element_by_tag_name('body').send_keys(Keys.LEFT_CONTROL, 'w')
sleep(2)
if tab_number == len(browser_instance.window_handles):
tab_number -= 1
browser_instance.switch_to.window(browser_instance.window_handles[tab_number])
if prior_current_tab == len(browser_instance.window_handles):
prior_current_tab -= 1
if prior_current_tab != tab_number:
if tab_number < prior_current_tab:
times = prior_current_tab - tab_number
else:
times = len(browser_instance.window_handles) - tab_number
times += prior_current_tab
if browser_type == "firefox":
action_chains = ActionChains(browser_instance)
action_chains.key_down(Keys.ALT)
for i in range(0, times):
action_chains.send_keys('`')
action_chains.perform()
else:
element = browser_instance.find_element_by_tag_name('body')
for i in range(0, times):
element.send_keys(Keys.LEFT_CONTROL, Keys.TAB)
browser_instance.switch_to.window(browser_instance.window_handles[prior_current_tab])
else:
if browser_type == "firefox":
print_info("The tab_number argument is None. Current window will be closed")
else:
print_info("The tab_number argument is None. Current tab will be closed")
browser_instance.find_element_by_tag_name('body').send_keys(Keys.LEFT_CONTROL, 'w')
if prior_current_tab == len(browser_instance.window_handles):
prior_current_tab = 0
browser_instance.switch_to.window(browser_instance.window_handles[prior_current_tab])
else:
status = self.close_browser(browser_instance)
return status
评论列表
文章目录