I’m having trouble accessing elements in a Hybrid App’s web view. My code handles multiple URLs, but when there are duplicate URLs, it goes to detached or hidden ones. Even though the URL I want is open, it remains detached in the list of handles. The code then tries to access the detached URL using the same locator, resulting in a boolean error. I only want to interact with the active URL. Since the DOM structure is the same for both URLs, using the same locator on a detached URL returns true and causes an error. I need a solution that allows me to access only the active URL, considering I have to navigate through hundreds of web views.
public static void cfnHandleActiveWebFinals(By locator) {
try {
Set<String> contextNames = driver.getContextHandles();
if (contextNames.size() > 1) {
driver.context((String) contextNames.toArray()[1]);
Set<String> windowHandles = driver.getWindowHandles();
List<String> handlesList = new ArrayList<>(windowHandles);
Collections.reverse(handlesList);
boolean foundWebURL = false;
for (String handle : handlesList) {
driver.switchTo().window(handle);
String currentURL = driver.getCurrentUrl();
boolean isWebURL = currentURL.startsWith("http");
boolean isElementDisplayed = verifyBoolean(1, locator);
if (isWebURL && isElementDisplayed) {
logger.info("Able to Find Final Page URL: " + currentURL);
foundWebURL = true;
break;
}
}
if (!foundWebURL) {
// printWarn("Unable to Find Valid web URL to Switch");
}
} else {
driver.context((String) contextNames.toArray()[0]);
}
} catch (UnhandledAlertException e) {
e.printStackTrace();
}
}