The heading says it all: Selenium fails when Chrome is hidden (I use a Mac where you can "hide" running applications) but succeeds when browser is visible.
What happens is that I, on page 1 (P1), send a click, which is accepted, and somehow Chrome/Chromedriver reports back that page 2 (P2) is done loading but when I try to find an element at P2 with if (driver.findElements(By.xpath(xpMail)).size() > 0)
it is not there, however long time I sleep the program before I check.
But, if I activate Chrome by bringing it to the front, the element is immediately found. This problem is specific to P1/P2. The script I run performs dozens of steps before and after P1/P2 without any problems, independently of if the browser is hidden or in visible the foreground.
Some simplified code:
do {
if (driver.findElements(By.xpath(xpMail)).size() > 0) {
Utilities.clearSendKeysAndWaitRandomLength(driver, xpMail, finalEmail, avgDelay);
} else {
Thread.sleep(500);
}
if (i%5 == 0 ) {
Toolkit.getDefaultToolkit().beep();
driver.get(URL);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(xpLand)));
Utilities.clickAndWaitRandomLength(driver, xpLand, avgDelay);
}
if (i > 25) {
break;
}
i++;
} while (variable == 0);
Any idea why this happens? Workarounds? Can I, programatically, somehow, just before this step, bring Chrome to the foreground and, afterwards, hide it again?