I'm trying to learn how to use C# Selenium to control Firefox. my question is how can I open a new tab and update an existing tab Url (and all of them are in same Firefox window) through C# Selenium?
Asked
Active
Viewed 439 times
1 Answers
0
To open new tabs you could use JavaScriptExecutor like this
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("window.open('https://www.google.com','_blank');");
To close the current tab
driver.findElement(By.cssSelector(“body”)).sendKeys(Keys.CONTROL + 'w');
To switch between tabs I use this method, you need to provide the window Tittle:
public void SwitchToWindow(Expression<Func<IWebDriver, bool>> predicateExp)
{
var predicate = predicateExp.Compile();
foreach (var handle in driver.WindowHandles)
{
driver.SwitchTo().Window(handle);
if (predicate(driver))
{
return;
}
}
throw new ArgumentException(string.Format("Unable to find window with condition: '{0}'", predicateExp.Body));
}
SwitchToWindow(driver => driver.Title == "Title of your new tab");

Jonxag
- 766
- 7
- 20
-
thanks. also is there any method like c# internal web browser events to run some code while a tab is loading such as DocumentCompleted or WebBrowserNavigated and etc.? – Sep 05 '19 at 06:35
-
I think it doesn´t have any event for this. Also you can check for an element of the new tab to know its state like this: driver.findElement(By.xpath("element")).isDisplayed() – Jonxag Sep 05 '19 at 07:50