How to close tab in WebDriver or Protractor

0 votes

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

I know how to switch tabs programmatically, but it does not bring the active tab to the foreground. I can't always tell what is going on in my E2E tests that run on SauceLabs because when I view the screencasts it is showing the tab that I navigated away from, not the active one.

it('should do something in the previous tab', function(done) {
    browser.getAllWindowHandles().then(function (handles) {
        browser.switchTo().window(handles[0]);
        // do something
        expect(something).toEqual(thisThing);
        done();
    });
});
Aug 16, 2018 in Selenium by Martin
• 4,320 points
4,209 views

1 answer to this question.

0 votes

Switch to the newly opened tab. Close the current windows (in this case, the new tab). 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]);
});
answered Aug 16, 2018 by Meci Matt
• 9,460 points

Related Questions In Selenium

0 votes
2 answers

How to scroll the Page up or down in Selenium WebDriver? (using java)

JavascriptExecutor jsx = (JavascriptExecutor)driver; jsx.executeScript("window.scrollBy(0,555)", ""); or Action classes ...READ MORE

answered Sep 6, 2020 in Selenium by Sri
• 3,190 points
18,506 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
12,755 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,624 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
6,699 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,561 views
0 votes
2 answers
0 votes
8 answers

How to open a link in new tab of chrome browser using Selenium WebDriver?

This below code works for me in ...READ MORE

answered Dec 14, 2020 in Selenium by Gitika
• 65,910 points
101,652 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP