I have a situation where I have to check whether after clicking on a link a new tab is opened or not. If the tab opens I want to check the title as well.
Does anyone have any idea about this.
Open a New Tab await driver. get('https://selenium.dev'); // A new tab is opened and switches to it await driver. switchTo(). newWindow('tab'); // Loads Sauce Labs open source website in the newly opened window await driver.
close() and driver. quit() are two methods for closing a browser session in Selenium WebDriver. It is necessary to know when to use each method in a test script.
Here is the same for C#; this is tested and will work whether the link opens in new tab or new window.
var browserTabs = driver.WindowHandles;
driver.SwitchTo().Window(browserTabs[1]);
//check is it correct page opened or not (e.g. check page's title or url, etc.)
// ...
//close tab and get back
driver.Close();
driver.SwitchTo().Window(browserTabs[0]);
Hope this helps anyone who comes across this thread.
You can use below method to implement the desired wait until the tab is fully loaded.
public static void switchTabs(WebDriver driver, int expectedWindowsCount,int SwitchtoWindow) throws Exception {
(new WebDriverWait(driver, 30)).until(ExpectedConditions.numberOfWindowsToBe(expectedWindowsCount));
ArrayList<String> tabs2 = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs2.get(SwitchtoWindow));
}
The method will check how many Active window handlers are available and wait until desired handlers are present and switch to the tab afterwards.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With