Close specific tabs in Firefox
glaine
Posted messages
33
Status
Member
-
NewStax Posted messages 7 Status Member -
NewStax Posted messages 7 Status Member -
Hello everyone,
First of all, I would like to thank in advance everyone who can help me. My problem is:
I want to close specific Firefox tabs in an open session:
The result is halfway to what I expect: the targeted tabs crash but do not close.
That's what I don't understand. Thank you again for your insights.
Best regards
First of all, I would like to thank in advance everyone who can help me. My problem is:
I want to close specific Firefox tabs in an open session:
$nbreIdFirefox= get-process -name Firefox|select-object -property ID
$nbreIdFirefox| foreach-object {if ($_ -ne $nbreIdFirefox[-1].Id){stop-process -Id $_.Id}}
The result is halfway to what I expect: the targeted tabs crash but do not close.
That's what I don't understand. Thank you again for your insights.
Best regards
1 answer
-
To close specific tabs on an open session in Firefox, you can use the Firefox control API (marionette). To do this, you first need to enable the "marionette" option in Firefox settings.
Here is an example of a PowerShell script that closes Firefox tabs using the Firefox control API (marionette):
# Import Selenium and ChromeDriver assemblies Add-Type -Path "C:\Selenium\WebDriver.dll" Add-Type -Path "C:\Selenium\ChromeDriver.dll" # Start Firefox $driver = New-Object OpenQA.Selenium.Firefox.FirefoxDriver # Navigate to a URL $driver.Navigate().GoToUrl("https://www.google.com") # Open a new tab $driver.ExecuteScript("window.open('https://www.bing.com')") # Retrieve the list of tabs $tabs = $driver.WindowHandles # Close the current tab (index 0) $driver.Close() # Switch to the next tab (index 1) $driver.SwitchTo().Window($tabs[1]) # Close the next tab (index 1) $driver.Close() # Quit the browser $driver.Quit()This script opens two tabs in Firefox, then closes the current tab (index 0) and the next tab (index 1). You can modify this script to close specific tabs using the tab index from the list of tabs.
I also recommend referring to the Firefox control API (marionette) documentation for more information on the various available features: https://firefox-source-docs.mozilla.org/testing/marionette/index.html. Keep me updated on your progress!