Is there a way to close a tab in WebDriver or Protractor?

后端 未结 8 1252
悲&欢浪女
悲&欢浪女 2020-12-01 06:37

Is there a way to physically close a tab via Protractor or WebDriver?

I ask because while I know how to switch tabs programmatically, but it does not bring the acti

相关标签:
8条回答
  • 2020-12-01 06:58

    if use keyboard library with selenium its very simple for switch on tab and close or etc :

    first install keyboard library :

    pip install keyboard
    

    second use keyboard for close tab in code for example :

        def change_tab_my_func(self, number):
            driver.switch_to.window(driver.window_handles[number])
    
        change_tab_my_func(0)   #your tab index    
        keyboard.send('ctrl+w')  
    
    0 讨论(0)
  • 2020-12-01 07:02

    You can try the following:

    1. Switch to the new opened tab.
    2. Close the current windows (in this case, the new tab).
    3. Switch back to the first window.

      browser.getAllWindowHandles().then(function (handles) {
      browser.driver.switchTo().window(handles[1]);
      browser.driver.close();
      browser.driver.switchTo().window(handles[0]);
      });
      
    0 讨论(0)
提交回复
热议问题