How to switch between tabs on a browser and get titles of each tab using Selenium?

0 votes
How to switch between tabs on a browser and get titles of each tab using Selenium?
Jul 22, 2019 in Selenium by Abhimanyu
3,464 views

1 answer to this question.

0 votes

Hi Abhimanyu, you can use following code snippet to switch between tabs on a browser and get titles of each tab using Selenium Webdriver:

System.setProperty("webdriver.chrome.driver", "C:\\Users\\Abha_Rathour\\Downloads\\chromedriver_win32\\chromedriver.exe");

driver = new ChromeDriver();

driver.manage().window().maximize();

driver.get("https://www.google.com/");

String link = "window.open('https://www.edureka.co','_blank');";

((JavascriptExecutor)driver).executeScript(link);

link = "window.open('https://www.facebook.com','_blank');";

((JavascriptExecutor)driver).executeScript(link);

for (String windowHandle : driver.getWindowHandles()) {

driver.switchTo().window(windowHandle);

System.out.println(driver.getTitle());

Thread.sleep(5000);

}
answered Jul 22, 2019 by Abha
• 27,930 points
with this code, we can switch to newly open now

1. how to switch the previously open tab.

2. If we closed a newly open tab previous tab still remains open and how to switch and how to close that tab

The following code snippet worked for me

 psdbComponent.clickDocumentLink();
    ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
    driver.switchTo().window(tabs2.get(1));
    driver.close();
    driver.switchTo().window(tabs2.get(0));

Related Questions In Selenium

0 votes
7 answers

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

Try this once driver.findElement(By.id("id")).sendKeys(Keys.CONT ...READ MORE

answered Sep 5 in Selenium by Sri
• 2,970 points
46,580 views
0 votes
1 answer

How to switch between two windows in browser using Selenium java

Yes, this is possible. First, you need ...READ MORE

answered Sep 25, 2018 in Selenium by Meci Matt
• 9,460 points
9,208 views
+1 vote
2 answers

How to get the title of a webpage using Selenium Java?

Essentially, driver.getTitle(); function can be used to ...READ MORE

answered Dec 17, 2018 in Selenium by Vardhan
• 13,180 points
6,684 views
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,510 points
3,562 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

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

answered Aug 31 in Selenium by Sri
• 2,970 points
4,567 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,890 points
2,611 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 2 in Selenium by Sri
• 2,970 points
2,904 views
0 votes
1 answer

How to open new tab in same browser and switch between them using Selenium?

Hi Mugdha, you can use following code ...READ MORE

answered May 24, 2019 in Selenium by Abha
• 27,930 points
9,338 views
0 votes
1 answer

How to get all options of a dropdown on a webpage using Selenium Webdriver?

Hey Joel, you can use following lines ...READ MORE

answered Jul 15, 2019 in Selenium by Abha
• 27,930 points
682 views