Nor Able to Close the frame | Selenium Forum
M
Posted on 06/06/2016
Hi,
I am not able to click on 'Close' button on the pop up please look into below code :-
[b:98cobnd4][u:98cobnd4]PLEASE REFER ATTACHED SCREEN SHOT WITH DOCUMENT : - [/u:98cobnd4][/b:98cobnd4]

[b:98cobnd4]Logic[/b:98cobnd4] :-

import java.sql.Driver;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
//import org.openqa.selenium.firefox.FirefoxDriver;

public class MMs_test {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://172.19.4.64/MR29/MMS/index.aspx");
Set<String> mainwindow = driver.getWindowHandles();
System.out.println("Total Numbers of the page --- > " +mainwindow.size());

Iterator<String> itr = mainwindow.iterator();
String FirstWindow = itr.next();
String SecondWindow = itr.next();
System.out.println("Parent Window id is --- >" +FirstWindow);
System.out.println("Child Window id is --- >" +SecondWindow);

driver.switchTo().window(SecondWindow);
driver.findElement(By.xpath("//*[@id='txtUserName']")).sendKeys("careteststh");
driver.findElement(By.xpath("//*[@id='txtPassword']")).sendKeys("irctc@116");
driver.findElement(By.xpath("//*[@id='btnLogin']")).click();

driver.findElement(By.xpath("//*[@id='5001']/div/span")).click();
driver.findElement(By.xpath("//*[@id='5002']/div/span")).click();
driver.findElement(By.xpath("//*[@id='5003']/div/span")).click();

List<WebElement> frame = driver.findElements(By.tagName("iframe"));
//System.out.println("Total Number Of Frmaes are -- > " + frame.size());
driver.switchTo().frame(0);
driver.findElement(By.xpath("//*[@id='aAddNewMatGroup']")).click();

frame = driver.findElements(By.tagName("iframe"));
System.out.println("Number Of Frame are-- > " +frame.size());
driver.switchTo().frame(0);
driver.findElement(By.xpath("//*[@id='txtGpName']")).sendKeys("AutoTest_Second_Third_t013034");
driver.findElement(By.xpath("//*[@id='btnSave']")).click();
driver.findElement(By.xpath("//*[@id='jqi_state0_buttonOk']")).click();
//driver.findElement(By.xpath(".//*[@id='TB_closeWindowButton']")).click(); ===>> [b:98cobnd4]Also try to get xpath with the id but it display no such elementException.[/b:98cobnd4]

((JavascriptExecutor)driver).executeScript("document.getElementById('TB_closeWindowButton').click();"); ====> [b:98cobnd4]Display Error :- Cannot read property 'click' of null[/b:98cobnd4]

//driver.switchTo().defaultContent();

}

}

M
Replied on 06/06/2016

use this way


https://www.seleniumeasy.com/selenium-tutorials/click-element-using-javascriptexecutor


M
Replied on 07/06/2016

I tried to used your logic which is mention on the link. I use JavaScript Function, using XPath with 'Title', 'getelement id' etc..Then i debug it
the problem is that ---- >> [b:25zm5nc8]"WebElement button = driver.findElement(By.xpath(".//*[@title='close or press Esc']"));"[/b:25zm5nc8].. [b:25zm5nc8] Not able to find that
element.[/b:25zm5nc8]

CODE :-


import java.sql.Driver;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
//import org.openqa.selenium.firefox.FirefoxDriver;

public class MMs_test {
static WebDriver driver;
public static void main(String[] args) throws Exception {

System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");
driver= new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://172.19.4.64/MR29/MMS/index.aspx");
Set<String> mainwindow = driver.getWindowHandles();
System.out.println("Total Numbers of the page --- > " +mainwindow.size());

Iterator<String> itr = mainwindow.iterator();
String FirstWindow = itr.next();
String SecondWindow = itr.next();
System.out.println("Parent Window id is --- >" +FirstWindow);
System.out.println("Child Window id is --- >" +SecondWindow);

driver.switchTo().window(SecondWindow);
driver.findElement(By.xpath("//*[@id='txtUserName']")).sendKeys("careteststh");
driver.findElement(By.xpath("//*[@id='txtPassword']")).sendKeys("irctc@116");
driver.findElement(By.xpath("//*[@id='btnLogin']")).click();

driver.findElement(By.xpath("//*[@id='5001']/div/span")).click();
driver.findElement(By.xpath("//*[@id='5002']/div/span")).click();
driver.findElement(By.xpath("//*[@id='5003']/div/span")).click();

List<WebElement> frame = driver.findElements(By.tagName("iframe"));
//System.out.println("Total Number Of Frmaes are -- > " + frame.size());
driver.switchTo().frame(0);
driver.findElement(By.xpath("//*[@id='aAddNewMatGroup']")).click();

frame = driver.findElements(By.tagName("iframe"));
System.out.println("Number Of Frame are-- > " +frame.size());
driver.switchTo().frame(0);
driver.findElement(By.xpath("//*[@id='txtGpName']")).sendKeys("AutoTest_Second_Third_t984021");
driver.findElement(By.xpath("//*[@id='btnSave']")).click();
driver.findElement(By.xpath("//*[@id='jqi_state0_buttonOk']")).click();

WebElement button = driver.findElement(By.xpath(".//*[@title='close or press Esc']")); ----> [b:25zm5nc8]During debug it throw exception
element not found.. so it not move to the safeJavaScriptClick(button); function[/b:25zm5nc8]

safeJavaScriptClick(button);

//((JavascriptExecutor)driver).executeScript("document.getElementById('TB_closeWindowButton').click();");
driver.switchTo().defaultContent();

}

public static void safeJavaScriptClick(WebElement element) throws Exception {
try {
if (element.isEnabled() && element.isDisplayed()) {
System.out.println("Clicking on element with using java script click");

((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);
} else {
System.out.println("Unable to click on element");
}
} catch (StaleElementReferenceException e) {
System.out.println("Element is not attached to the page document "+ e.getStackTrace());
} catch (NoSuchElementException e) {
System.out.println("Element was not found in DOM "+ e.getStackTrace());
} catch (Exception e) {
System.out.println("Unable to click on element "+ e.getStackTrace());
}
}
}

Responsive image

M
Replied on 07/06/2016

????


M
Replied on 07/06/2016

Hi,
Ignore it i have resolved it on my on.....


Responsive image


M
Replied on 07/06/2016

great what worked?


M
Replied on 08/06/2016

Hi Ashish,

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class MMS_Material_Group_ADD {
static WebDriver driver;
public static void main(String[] args) throws InterruptedException {

System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");
driver= new ChromeDriver();
//driver= new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://172.19.4.64/MR29/MMS/index.aspx");
Set<String> mainwindow = driver.getWindowHandles();

Iterator<String> itr = mainwindow.iterator();
String FirstWindow = itr.next();
String SecondWindow = itr.next();

driver.switchTo().window(SecondWindow);
driver.findElement(By.xpath("//*[@id='txtUserName']")).sendKeys("careteststh");
driver.findElement(By.xpath("//*[@id='txtPassword']")).sendKeys("irctc@116");
driver.findElement(By.xpath("//*[@id='btnLogin']")).click();

driver.findElement(By.xpath("//*[@id='5001']/div/span")).click();
driver.findElement(By.xpath("//*[@id='5002']/div/span")).click();
driver.findElement(By.xpath("//*[@id='5003']/div/span")).click();

List<WebElement> frame = driver.findElements(By.tagName("iframe"));
driver.switchTo().frame(0);
driver.findElement(By.xpath("//*[@id='aAddNewMatGroup']")).click();

for(int i=0;i<frame.size();i++){
try
{
driver.switchTo().frame(i);
System.out.println("Switched to frame");

}
catch(Exception e)
{
// System.out.println("Not in this frame:"+ i);
// driver.switchTo().defaultContent();
}
}
driver.findElement(By.xpath("//*[@id='txtGpName']")).sendKeys("AkashTest");
driver.findElement(By.xpath("//*[@id='btnSave']")).click();
[b:jbnyrtp7]driver.findElement(By.xpath("//*[@id='jqi_state0_buttonOk']")).click(); ============ >>>> Problem Was after that line
driver.switchTo().defaultContent(); ------- > i was using this command in wrong line and after that i used Actions Class. And it works. Responsive image
Thread.sleep(1000L);
WebElement close = driver.findElement(By.xpath("//a[@id='TB_closeWindowButton']"));
Actions act = new Actions(driver);
act.moveToElement(close).build().perform();
driver.findElement(By.xpath("//a[@id='TB_closeWindowButton']")).click();[/b:jbnyrtp7]

}}