How do we get the currently running Chrome browser instance count?
from selenium import webdriver
driver = webdriver.Chrome(chrome_options=options)
Since there is limitation on maximum instance count in Chrome, I need to get the current instance count before proceeding.
Suppose this would be the same as the number of window handles you have, so try with len(driver.window_handles)
Related
Recently, I have upgraded my Selenium version from 2.53 to 4.1.2 to enable testing of our application on MS EDGE IE11. But we are intermittently facing issues while retrieving number of windows open in MS EDGE IE11 with selenium-4.1.2
Did anyone else facing similar kind of issues with Selenium-4.1.2 ?
Below is piece of code I have tried on MS EDGE IE11. Sometimes we could see its returning correct no. of windows but sometime not. We are also using sufficient wait-time before retrieving number of windows.
Note - This is working absolutely fine on IE11 browser with Selenium-4.1.2
int noOfWindowsOpen = driver.getWindowHandles().size();
Expectation : It should always return correct value of no. of windows open.
Once you open the new tab / window before you count the number of WindowHandles you need to induce WebDriverWait for numberOfWindowsToBe() as follows:
driver.get("http://www.google.com");
((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");
new WebDriverWait(driver, 10).until(ExpectedConditions.numberOfWindowsToBe(2));
int noOfWindowsOpen = driver.getWindowHandles().size();
It seems to be a known limitation in automating Edge IE mode. It says:
To ensure the new window has been created successfully and IEDriver
has detected it, you must continuously check the result of the Get
Window Handles command until it contains a handle to the new window.
You can try the sample code in it:
int initialHandleCount = driver.getWindowHandles().size();
driver.findElement(By.id("<Id of the button that will open a new window>")).click();
Set<string> newHandles = driver.getWindowHandles();
while (newHandles.size() == initialHandleCount) {
newHandles = driver.getWindowHandles();
}
I have found a work-around for above problem.
Before executing, make sure all the instances of ME Edge, IE and IE Driver are closed. If not, kill them forcefully from task manager and then re-run the Test Script. Script will identify the new window properly.
Thanks
I use the following code to run Tor controlled by Selenium in Lubuntu:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
torexe = os.popen(r'/home/sergey/.local/share/torbrowser/tbb/x86_64/tor-browser_en-US/Browser/firefox')
profile = FirefoxProfile(r"/home/sergey/.local/share/torbrowser/tbb/x86_64/tor-browser_en-US/Browser/TorBrowser/Data/Browser/profile.default")
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9050)
profile.set_preference("network.proxy.socks_remote_dns", False)
profile.update_preferences()
driver = webdriver.Firefox(firefox_profile=profile, executable_path=r'/usr/local/bin/geckodriver')
driver.get("https://www.google.com")
...
It works, but in somewhat strange manner. Running it results in opening two windows, one window is Tor's while the other one being that of Firefox. Selenium controls the FF window only. The Tor window just sits there idly.
It is not to say that makes life unbearable, as I have said every thing works, but I am merely curious to know how to «make it completely right» (by this, I mean executing Selenium script in the only window of Tor).
I figured it out. In order to launch Tor using Selenium please run the following code (it still launches Firefox, this time without any Tor windows, but it uses the Tor service):
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9050)
profile.set_preference("network.proxy.socks_remote_dns", False)
profile.update_preferences()
driver = webdriver.Firefox(profile)
You may want to add the following three lines to verify the Tor service provides new IP for you Firefox instance:
driver.get("http://icanhazip.com")
current_IP = driver.find_element_by_css_selector("body > pre:nth-child(1)")
print(current_IP.get_attribute("innerHTML"))
This site icanhazip.com allows one to see his external IP without necessity to pass the abominable captcha test.
In Chrome, shift-escape gives you Chrome's task manager.
In the task manager, you can see various stats, including the "memory footprint" of a tab.
I'd like to get that value in Selenium. Is there a way to do so?
Apparently there is a chrome.processes API which looks like it could be useful, if it could be accessed from Selenium.
You can execute arbitrary JavaScript code from Selenium therefore you can access i.e. Window.Performance object in general and Windows.Performance.Memory object in particular
Example code:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://accounts.seetest.io/signup')
print(driver.execute_script("return window.performance.memory"))
driver.quit()
Example output:
{'jsHeapSizeLimit': 2197815296, 'totalJSHeapSize': 23449360, 'usedJSHeapSize': 14905688}
I've tried the standard
var elementForMs = driver.findElement(By.xpath(selector));
driver.executeScript("arguments[0].click()", elementForMs);
and
var elementForMs = driver.findElement(By.css(selector));
driver.executeScript("arguments[0].click()", elementForMs);
And there are simply cases where the element never responds to the click in Microsoft Edge 15.01563.
Each driver has unique bugs. So somethings that work in Firefox, may not work in Chrome or so on. So the only way around is to find what works and use it. And if possible report the issue to the driver owner
In your case since finding the element and clicking on it doesn't work with
var elementForMs = driver.findElement(By.xpath(selector));
driver.executeScript("arguments[0].click()", elementForMs);
But works when you use javascript directly in console. that means you should execute the same in your code
driver.executeScript("document.getElementXXX().click()");
I am working on a Selenium test project where I need to launch two browsers at initial setup .
Then I need to do switching between these browsers.
So I will have [Window1] [Window2]
I would like to run test through [Window1] and then switch to [Window2] to check result of actions done in [Window1]
Any idea on how to do it?
I tried driver.switchTo().window() but no luck.
Any help would be greatly appreciated. Thanks
driver.switchTo().window() will work only if new window is opened by any action in existing window. If you are using different drivers to open different windows then it wont work.
In such case you need to choose appropriate instance of driver to control the new window.
Suppose you have instance of webdriver
// Window 1
WebDriver chrome = new ChromeDriver()
// Window 2
WebDriver firefox = new FirefoxDriver()
Now use chrome whenever you want to interact with Window 1 and use firefox to interact with Window 2.
Just use two driver instancess:
WebDriver driver1 = new ChromeDriver()
WebDriver driver2 = new FirefoxDriver()
You can make them both same flavour if you want.
You need to pass the parameter as window name or you can get all the window handles and then switch to the particular window handle.
You could use:
driver.switchTo().window("windowName");
or:
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}