Selenium is unable to find element in a Modal window with iframes - java

I have a page in which I click on a link which opens a new Modal window which has an iframe. I switched to the iframe and performed some validation, then click on the link in that Modal window which in turn opens a second new Modal window with an iframe. I am facing issue clicking on any element in that second new Modal window.
Here is my code.
WebElement Hotelname = driver.findElement(By.cssSelector(".hotelTitleZone2>a"));
Hotelname.click(); \\This will open a new Pop up.
driver.switchTo().frame(1);
\\perform some validation
String parentHandle = driver.getWindowHandle();
driver.findElement(By.linkText("View on a Map")).click(); \\This will open second pop up Modal window
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
driver.switchTo().defaultContent();
driver.switchTo().frame(1); \\switching to frame
driver.findElement(By.linkText("Close")).click();
When I am running this code, I am getting an error:
org.openqa.selenium.NoSuchElementException: Unable to locate element:
{"method":"link text","selector":"Close"}
I tried with or without switching to default content, without switching to frame in second Modal window but result is always the same.
Any help is appreciated ? Thanks.

My understanding is:
start from the default window
click to open the first Modal window that has an iframe
Switch to this new iframe (index = 1)
Get the ID for the current window handle, which is the default window handle
click to open the second Modal window that has a second iframe
switch to the second Modal window
switch back to the default window
switch to iframe (index = 1)
Find the button you are after
There are a few confusions here:
in step 4 above, you used String parentHandle = driver.getWindowHandle(); to store the original default window handle but you have never used it, instead, you use driver.switchTo().defaultContent();
What happened to the first Modal window after you clicked it? Did it close? if it had not closed, its iframe would still be iframe (index=1), this would explain why you could not find your button from the iframe (index=1); as your button would reside on the iframe that belongs to the second Modal window, which is likely to be iframe (index=2). You may use driver.switchTo().frame(2); to address it instead. To be sure, you can inspect HTML elements to see how many iframes are present and to which Modal windows they belong to.
Hope you will find it useful.

Related

How to click on a confirm button inside a modal that's a pop up

I want to click on the "Confirm" button in the pop up inside a modal. But when I click on the captured webElement
Example:
#FindBy(xpath = "//div[#class='modal-footer']//button[text()='Confirm']")
WebElement clickOnConfirmButton;
No such element exception error is displayed.
I am attaching the screenshot of the html as well as the webpage on which I am working.
Note:
When I try to find the element using xpath on the console using $x() of the browser, it gets detected. However when I try to execute my code using the same xpath it throws a no such element exception.
Warning modal is a modal inside a modal.
please check if there is any frame on Dom ,if it is there then just switch to the frame and perform the action.
If frame is not exists the go for css selector along with shycronisation
Solution:1
You need to introduce webDriverWait here i.e visibility of element before performing any action.
Solution:2
You should try to switch to alert and then press on the confirm button.
driver.switchTo().alert().accept();

Selenium webdriver , testNG with java

How to handle the Modal window?Selenium webdriver , testNG with java
For example:
Load https://business.bell.ca/shop/small-business/
Click on Share Via email icon below the Facebook icon on right hand side.
Modal window is displayed
How to handle this modal window as i need to take the Screen shot of that modal window?
There isnt any modal window. If you are trying to click on Like, its under an iframe. To switch to it perform:
driver.findElement(By.cssSelector(".fui-icon.fui-icon-facebook"))
.click();
driver.switchTo().frame(
driver.findElement(By.xpath("//iframe[#title='facebook']")));
driver.findElement(By.xpath("//span[.='Like']")).click();
and to switch to facebook window that follows do:
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
EDIT:
Sorry it was my mistake that i took didn't got what you tried to ask. As a workaround if you want to interact with the modal dialog you could use by initially waiting for the modal-dialog to appear, and since its under top-window scope only, you can interact with the fields using xpath or css, whichever you prefer. A sample code for it with xpath would be:
driver.findElement(By.id("shareemail")).click();
new WebDriverWait(driver, 10).until(ExpectedConditions
.visibilityOfElementLocated(By
.xpath("//*[#id='emaillightboxmodaljs']")));
driver.findElement(
By.xpath(".//*[#id='ui-id-3']/div/fieldset/div[1]/div[1]/input"))
.sendKeys("acd");

Opening a link in new window

I need to open a link in a new window with Selenium. I found the following 2 (assume we is the WebElement and I have already found it and myWait just performs a wait for x milliseconds):
1)
Actions act = new Actions(driver);
act.contextClick(we).perform();
myWait(1000); // allow the menu to come up
act.sendKeys(which).perform();
2)
we.sendKeys(Keys.CONTROL + "t");
In #1 I see the menu come up, but the sendKeys does not work (oh, by the way the "which" in sendKeys is a "t"). In #2 it simply ignores it.
In #1 is the menu that comes up in a new window already? Do I have to driver.switchTo()? or if not, what am I doing wrong?
Also, is there another way to do this? we is an element of form <a href=blah> and a regular click opens fine.
You can try the below code to open a link in a new window:
Actions newTab= new Actions(driver);
WebElement link = driver.findElement(By.xpath("//xpath of the element"));
//Opening the link in new window
newTab.contextClick(link).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
Note: After you've opened the link a new window, to get control of it, you need to get the handle of this child window, switch to it using driver.switchTo.window("childwindowhandle") and take further actions. And, after that you can close it and switch back to the Parent window(original one) and take actions here (Make sure to get the parent window handle before you open the link in a new window, so that switching back to it is easier).

Switch between browser tabs using Selenium WebDriver with Java

I need to switch between the browser tabs, used the following code,
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
It was working properly sometimes only, but sometimes it is showing an exception.
Can anyone suggest me is there any other instructions for switching tabs within a single window by using java.
You have to use window handle function here. Here is a sample working code in java:
String parentHandle = driver.getWindowHandle(); // get the current window handle
System.out.println(parentHandle); //Prints the parent window handle
String anchorURL = anchor.getAttribute("href"); //Assuming u are clicking on a link which opens a new browser window
anchor.click(); //Clicking on this window
for (String winHandle : driver.getWindowHandles()) { //Gets the new window handle
System.out.println(winHandle);
driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
}
//Now your driver works on the current new handle
//Do some work here.....
//Time to go back to parent window
driver.close(); // close newly opened window when done with it
driver.switchTo().window(parentHandle); // switch back to the original window
Hope this helps!
Switching between browser window is different from switching b/w tabs.
In some browser windowhandler command may work but it wont work in all browser.
Here is the solution to navigate b/w tabs
for navigating left to right side:
Actions action= new Actions(driver);
action.keyDown(Keys.CONTROL).sendKeys(Keys.TAB).build().perform();
For navigating right to left :
Actions action= new Actions(driver);
action.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).sendKeys(Keys.TAB).build().perform();
In my case, the following code is working fine-
String oldTab=driver.getWindowHandle();
driver.findElement(pageObj.getL_Popup_Window()).click();
ArrayList<String> newTab = new ArrayList<String>(driver.getWindowHandles());
newTab.remove(oldTab);
driver.switchTo().window(newTab.get(0));
WebElement ele = driver.findElement(pageObj.getI_input_name());
ele.click();
ele.sendKeys(name);
driver.findElement(pageObj.getI_submit()).click();
driver.switchTo().window(oldTab);

Unable to switch back to the parent window from the popup frame using selenium webdriver

I have the login page, when click login its opens the new tab. I moved the control to new tab using
driver.switchTo().window("");
// Done some stuffs in new window.
when I click one Button it will open the popup (frame).
I have selected a popup window using driver.switchTo().frame("frameName");
and from there by selecting the record, the popup window will be closed. The selected record will be displayed in parent window and the page got refreshed.
Now I want to return(Re-Focus) the control to my parent window for doing the some other stuffs.
but I could not focus the parent window again.
I have tried:
driver.switchTo().defaultcontent();
driver.switchTo().window("");
and
driver.getWindowHandles();
Still the same result....
Could anyone please help me on this....
String oldWindow = driver.getWindowHandle();
driver.findElement(By.xpath("//a[#id='searchSalesImg']/img")).click();
//by clicking on this we will get a new window
Thread.sleep(5000);
driver.switchTo().window(driver.getWindowHandle());
driver.switchTo().window("Employer Services Order Management (ESOM)");
//Navigate to new window
driver.findElement(By.id(" Primary Sales Person:")).sendKeys("MAS%");
//Do some stuff in new window
driver.switchTo().window(oldWindow);
//navigate back to old window
Try using this code:-
driver.switchTo().frame("Frame_identifier");
//your code
String winHandleBefore = driver.getWindowHandle();
webDriver.SwitchTo().Window(winHandleBefore);

Categories

Resources