browser pop up is not closing in selenium - javascript

After launching the url there is a email pop up that i am trying to close. i have written xpath and able to find the close icon with that. but when i am trying to execute it is not closing. then i added implicitly wait for the element to be visible and then trying to close. still it is not closing.
Can you please tell me what would be the reasons for such cases and how it can be solved.
and Submit button is also not clicking i have given the correct xpath.
Thanks in advance..
below is the code snippet.
public static void main(String[] args) {
WebDriver driver;
System.setProperty("webdriver.chrome.driver", "E:\\Softwares\\Chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://html.com/input-type-file/");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
WebElement closeButon = driver.findElement(By.xpath("//a[#class='om-close miami-element-close miami-close']"));
if(closeButon.isDisplayed())
{
System.out.println("close Buton is there.. ");
closeButon.click();
System.out.println("close Buton closed ");
}
driver.findElement(By.xpath("//input[#name='fileupload']")).sendKeys("E:\\Users\\laxman_p\\Desktop\\PromoFeature.txt");
//Submit button
driver.findElement(By.xpath("//*[#id='post-206']/div/div[3]/form/input[2]")).click();
}

If you cant close it by xpath,
I think this issue can be related with focused window,
You need to use getWindowHandle and switchTo methods to focus this pop-up.
After that you will be able to make process on this pop-up.
driver.getWindowHandles()
returns set of windows.
driver.switchTo.window(windowId);
will switch you to window which you want to focus on.

Related

How to accept alert in selenium which is coming up in a different window?

Scenario: There are 2 windows open. When I am clicking a button on the 2nd window, a 3rd window is opening and the focus is automatically shifting to 3rd window. An alert is coming on the 3rd window to accept.
Issue: I am not able to accept the alert as it's coming in a different window.
Findings: I think it's the limitation of Selenium. If the alert is coming on the same window where the button is clicked, we have the DOM, so we are able to interact with the alert. But in this case the alert is in a different window, so the state of the browser is locked.
Tried solution: Tried all the possible ways by using javascript, selenium action class etc, but it's not working.
some of the tried methods are as below
//e.click();
/*Actions ac = new Actions(driver);
ac.sendKeys(Keys.ENTER).build().perform();*/
String onClickScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('click', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject){ arguments[0].fireEvent('onclick');}";
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript(onClickScript, e);
/* Actions asd = new Actions(driver);
asd.clickAndHold(e).perform();
Thread.sleep(1000);
asd.release().perform();*/
To clear certain doubts an Alert is generated through JavaScript and is never a part of the HTML DOM.
To accept or dismiss an Alert you must always induce WebDriverWait for the alert to be present as follows:
import org.openqa.selenium.Alert;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
//other code
Alert myAlert = new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
//accept an alert
myAlert.accept();
//dismiss an alert
myAlert.dismiss();
The below solution is working fine and can be used in similar scenario.
We have to use the Robot class of java.awt package.
In the below code Alt+space+c will close any opened window. Here closing the alert.
public void closeAlert(String strControlName, String delayTime) {
Robot rb;
int timeInSec = Integer.parseInt(delayTime);
try {
rb = new Robot();
rb.keyPress(KeyEvent.VK_ENTER); //for clicking on the button or link
rb.keyRelease(KeyEvent.VK_ENTER);
Log.info("Wait for "+timeInSec+" Secs");
Thread.sleep(timeInSec*1000);
rb.keyPress(KeyEvent.VK_ALT);
rb.keyPress(KeyEvent.VK_SPACE);
rb.keyPress(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_SPACE);
rb.keyRelease(KeyEvent.VK_ALT);
Log.info("Successfully clicked on '"+strControlName+ "' and closed the Alert");
} catch (Exception e) {
Log.info("Failed click on '"+strControlName+ "' and close the Alert");
}
}

Clicking an image button using selenium webdriver after login

I'm trying to automate a login page using selenium-webdriver and I'm using IE as the browser. The problem that I'm experiencing is that after the login I'm not able to select the image button. I have even introduced time delay after the login credentials are entered still the script runs and the button doesn't gets clicked. P.S I have commented all the methods I have tried too.
The code I have used is:
System.setProperty("webdriver.ie.driver",
"C:/Program Files/IEDriverServer/IEDriverServer.exe");
WebDriver driver=new InternetExplorerDriver();
driver.manage().window().maximize();
driver.navigate().to("website name");
driver.findElement(By.id("userid")).sendKeys("username");
driver.findElement(By.id("password")).sendKeys("password");
//driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.findElement(By.id("submitButton")).click();
Thread.sleep(5000);
//driver.findElement(By.id("imgBtnAdd")).click();
//driver.findElement(By.cssSelector("a[href='Images/Go.gif']")).click();
driver.findElement(By.xpath("//img[# src='Images/Go.gif']")).click();
//System.out.println("Manual Click of Ok button");
//By xpath = By.xpath("//button[#name='imgBtnAdd'][#type='image'][contains(image(),'Images/Go.gif')]");
//WebElement myDynamicElement = (new WebDriverWait(driver, 10))
//.until(ExpectedConditions.presenceOfElementLocated(xpath));
//myDynamicElement.click();
driver.findElement(By.cssSelector("input[id='rblRoleGroup_4']")).click();
driver.findElement(By.id("imgBtnRoleGroup")).click();
//WebElement element = driver.findElement(By.id("imgBtnAdd"));
//Thread.sleep(5000);
//JavascriptExecutor executor = (JavascriptExecutor)driver;
//executor.executeScript("arguments[0].click();", element);
//driver.close();
You can try sendkkeys :
driver.findElement(By.id("imgBtnAdd")).sendKeys(Keys.ENTER);
If image is in new window try this:
for(String winHandle : driver.getWindowHandles())
{
driver.switchTo().window(winHandle);
}
Try click using JavascriptExecuter might help you -
WebElement element = driver.findElement(By.id("imgBtnAdd"));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);
After viewing your code, I have noticed that your xpath was wrong.
Instead of using <img> tag, use <input> tag.
driver.findElement(By.xpath("//input[#src='Images/Go.gif']")).click();
Explanation of xpath:- Use src attribute of <input> tag.

Close Flipkart open pop-up and go to main window using Selenium

WebDriver driver = new FirefoxDriver();
driver.get("https://www.flipkart.com");
driver.manage().window().maximize();
String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
String subWindowHandler = null;
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler);
I tried it by switching to main window also. Please add valuable input or code to close the pop up.
You can try using the java Robot API by importing java.awt.Robot libraries. An example is here:
One solution for File Upload using Java Robot API with Selenium WebDriver by Java
You can try to use it similarly to press the Esc key. Pressing Esc on flipkart website gets rid of the pop-up.
The pop-up which appears on Flipkart's website is a simple HTML modal. Window handle is used when a new pop-up window needs to be accessed.
To close the pop-up just click on the cross on the top right corner of the pop-up. Use waits to ensure that selenium finds the WebElement.
Try this:
driver.get("https://www.flipkart.com");
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement cross = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.className("close-icon")));
cross.click()

Selenium webdriver doesn't click on save button in chrome and firefox

My project is in mvc and I want to test it using selenium web driver.
Some button clicks work properly. But when I navigate to different page saying continue on that page Save button doesn't work.
Below is my code
driver.FindElement(By.Id("BtnAddNew")).Click();
IWebElement cat = driver.FindElement(By.Id("Cat"));
cat.SendKeys("Single-family house");
IWebElement ext_id = driver.FindElement(By.Id("ExternalId"));
ext_id.SendKeys("SAR_47");
IWebElement zip = driver.FindElement(By.Id("AddressZipTown"));
zip.SendKeys("1205 Genève");
IWebElement street = driver.FindElement(By.Id("AddressStreet"));
street.SendKeys("Tramstrasse 10");
driver.FindElement(By.Id("btnContinue")).Click();
driver.FindElement(By.Id("btnSave")).Click();
driver.Quit();
Can anyone solve my problem?
You can solve this problem by checking the DOM for a certain amount of time to try to find your WebElement. You can use Implicit or Explicit Waits.
Doc:
http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#explicit-waits
http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#implicit-waits

close the window popup using JS

Writng script for closing window which i get as a pop up in any website for example
if u launch www.myntra.com it gives a window like if u have facebook acount login.. am trying to close this facebook window
HTML code:
<div class="close"></div>
frist i have tried normal webdriver script
driver.findElement(By.xpath("//div[#class='close']").click;
above code gives me exception like "element is not visible you may want to interact with"
i have tried using javaexecutor somthing like below'
WebElement element = driver.findElement(By.xpath("//div[#class='close']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
above JS code doesnot give me exception but`enter code here` even it doesnot close the window for which i have written the code.
1)why in above webdriver code i get exception like "element is not visible you may want to interact with"? what is reason behind this.
2) what is the way to close the window?
i have spent whole day but dint got solution please help me out thanks in advance
You can close the browser window with Selenium, without Javascript.
// get the current windowHandle before triggering the popup (if loading myntra triggers the popup, just navigate to localhost and get the windowHandle there, before navigating to myntra)
String window1 = driver.getWindowHandle();
// trigger the popup
driver.get("www.myntra.com");
// get all window handles
String[] handles = driver.getWindowHandles().toArray(new String[2]);
// switch focus to the popup window
driver.switchTo().window(handles[handles.length - 1]);
// close the popup
driver.close();
// switch focus back to the original window
driver.switchTo().window(window1);
// continue test...

Categories