I am trying to refresh my Selenium knowledge. So, I am writing a script to navigate through my Google account. After I successfully sign in to Google, a pop up appears under my profile icon on the upper right side in Firefox. I no longer get these pop ups when I sign manually. This is probably because of cookies or some other browser setting. I do not care about the reason why this occurs.
However, since it occurs, I believe it may be preventing my script from closing the browser with driver.close(); I also tried driver.quit(); Neither of these are causing the browser to close.
So, I thought I would try switching windows by doing an iteration through the windows. This is not allowing me to select the pop up that appears to close it.
I also tried to create an Alert alert and switch to it:
driver.switchto.alert();
driver.dismiss();
This is not dismissing this pop up in Google either.
In the end, I do not care about this pop up. I know I listed 2 separate issues here. But, in the end, I just want to close the browser. If I can also learn how to switch to this pop up and click the x to close it, that is a bonus.
//Code added here -------------------------
public void sign_out( WebDriver driver )
{
//At this point,I am already signed in. But, that Google pop up appears
//The pop up says "Get to Google faster. Switch your default search engine to Google."
//Wait for "x" to appear
myDynamicElement = (new WebDriverWait(driver, wait_for_element_time)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(x_path_x_pop_up)));
//This is set above as a class member
//final String x_path_x_pop_up = "/html/body/div/div[3]/div[1]/div/div/div/div[2]/div[4]/div/a";
WebElement x_icon = driver.findElement(By.xpath(x_path_x_pop_up));
//Click x to close it
x_icon.click();
String myWindowHandle = driver.getWindowHandle();
String subWindowHandle = null;
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandle = iterator.next();
}
driver.switchTo().window(subWindowHandle); // switch to popup window
// driver.switchTo().window(myWindowHandle);
// This never happens now on this click of the profile icon in Google to sign out. //Click on profile icon m_profile_icon.click();
//Wait for "Sign Out" button to appear
myDynamicElement = (new WebDriverWait(driver, wait_for_element_time)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(x_path_sign_out_button)));
//Get sign out button
WebElement sign_out_button = driver.findElement(By.xpath(x_path_sign_out_button) );
sign_out_button.click();
}
}