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");
Related
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();
How to make post on Facebook using Selenium Webdriver + Java?
This window. http://prntscr.com/i603dv
My code:
driver.findElement(By.xpath("//input[#id='email']")).sendKeys("me-email");
driver.findElement(By.xpath("//input[#id='pass']")).sendKeys("my-password");
driver.findElement(By.xpath("//label[#id='loginbutton']")).click();
//here I turn off notifications
driver.findElement(By.xpath("//a[#action='cancel']")).click();
Actions actions = new Actions(driver);
//with help of this code I can focus on window.
WebElement element = driver.findElement(By.xpath("(//span[#class=\"uiIconText _5qtp\"])[1]"));
actions.doubleClick(element);
//Here I want to post text, but it doesn't work.
driver.findElement(By.xpath("//div[#class=\"_1mf _1mj\"]")).sendKeys("Test");
Why it doesn't work? Then I press button Post but it doesn't work also.
The popup/window opens after double click . So you could try to switch to new popup using switch to window of webdriver. It should help .
Here is a link of simple webdriver test which switch window
http://learn-automation.com/handle-multiple-windows-in-selenium-webdriver/
Try to do the same for you .
If you have any questions let me know .
"_1mf _1mj " is a complex class. Selenium won't work on complex classes.
You can try Action chain - press "tab" button multiple times until you reach to "post"
Also, Facebook provides a drop down option after pressing tab button . You can choose from the drop down what you want to do., Using selenium.
Alternatively try mbasic version of FB.
I am new in Slenium.
I am trying to handle the pop-up Form.
When I click in New Button the pop-up form like this will open.
I try the handle this by alert(), pop up handling and also by Child Browser Handling. But didn't get the solution.
Please suggest some solution for this issue
If it's an <iframe> element then you need to switch WebDriver to this frame in order to work with it. Here's an example of how you can do this:
By locIframe = By.xpath("//iframe[#name='popup']");
driver.switchTo().frame(driver.findElement(locIframe));
// driver is an instance of RemoteWebDriver
The Xpath locator is just an example: you need to write your own here. Also you can use any other locator to find that <iframe> element in the page source.
After switching to iframe element WebDriver will see it's page source and will be able to work with it.
I also faced the similar issue. When clicked on EDIT Button in the parent window, an iFrame pop-up appears as shown below:`
As mentioned that you tried to handle using alert() or pop up handling and also by Child Browser Handling. But none of them will work since it is an iFrame pop-up. So you need to switch WebDriver to this frame in order to work with it.
For my case I did using the following code:
driver.switchTo().frame(3);
To find the index of the frame you can use Selenium IDE.
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.
I have an application in which i tried clicking on a button and in return it will pop-up a window for filling a new user form. This is not really like a pop-up window, because it has some input fields as well as "save " and "cancel" button. It looks similar to pop-up window in facebook.
Here the code i tried with
Set beforePopup = driver.getWindowHandles();
driver.findElement(By.xpath("/html/body/div/div/div/div[3]/div/div[2]/div[2]/table/tbody/tr/td[3]/table/tbody/tr/td[2]/em/button")).click();
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
Set afterPopup = driver.getWindowHandles();
afterPopup.removeAll(beforePopup);
if(afterPopup.size() == 1) {
driver.switchTo().window((String)afterPopup.toArray()[0]);
}
//driver.switchTo().window("Add New User");
//selenium.type("userDetailsBean.firstName","alan1");
//WebElement btnStartQueryInside = driver.findElement(By.xpath("//html/body/div[14]/div/div/div/div/span"));
//btnStartQueryInside.getText();
System.out.println(driver.getTitle());
WebElement firstName = driver.findElement(By.xpath("/html/body/div[2]/div/div/div/div/div/form/div/div/input"));
firstName.sendKeys("alan1");
//driver.switchTo().alert();
//driver.findElement(By.xpath("//html/body/div[14]/div/div/div/div")).getText();
//WebElement firstName=driver.findElement(By.xpath("/html/body/div[2]/div/div/div/div/div/form/div/div/input"));
//firstName.sendKeys("alan1");
/*WebElement lastName=driver.findElement(By.id("userDetailsBean.lastName"));
lastName.sendKeys("harper");
WebElement emailadd=driver.findElement(By.id("userDetailsBean.userEmail"));
emailadd.sendKeys("alan1#derik.com");
WebElement username=driver.findElement(By.id("userDetailsBean.userName"));
username.sendKeys("xalan1");
WebElement password=driver.findElement(By.id("adminPassword"));
password.sendKeys("Derik123");
WebElement repassword=driver.findElement(By.id("adminPassword2"));
repassword.sendKeys("Derik123");
driver.findElement(By.xpath("//html/body/div[2]/div/div/div/div/div[2]/div/div/table/tbody/tr/td/table/tbody/tr/td[2]/em/button")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);*/
Please note that in code I commented some input field filling, because I thought first I will make it working just for 1st field. Please help me how to proceed.
the problem is after clicking the pop-up button, I'm not sure if the control switches to pop-up window or not. the gettitle after pop-up gives the main window title. and it is not able to find the first input field using the xpath or id.
The term window in Selenium means the actual browser window, not frames or just divs. Therefore, your logic is wrong, getWindowHandles() and driver.switchTo().window("Add New User") are not what you are after.
What you need is driver.switchTo().frame().
driver.switchTo().defaultContent(); // optional, use only if driver is already in an iframe
WebElement editUserForm = driver.findElement(By.cssSelector("iframe[src*='editUserForm']"));
// there are other overloads (by frame name, index, id) and locators can be used here.
driver.switchTo().frame(editUserForm);
// make sure your locator here is correct
WebElement lastName = driver.findElement(By.id("userDetailsBean.lastName")); // I doubt this is correct
// from your screenshot, I'd suggest By.cssSelector("[id*='userDetailsBean.lastName'] input")
lastName.sendKeys("harper");
Also just a kindly heads up, your code smells.
Your implicitlyWait wait usage seems wrong, please read the
documentation and the post.
Where did you get selenium.type("userDetailsBean.firstName","alan1");? Did you just copy the line from somewhere else? This looks like Selenium RC to me.
Why driver.switchTo().alert()? Have you read the documentation on what's this for?
Please don't use absolute xpath.
Try use page object if you haven't done that in your real project. (It's fine if you just want to illustrate the problem here)