How to change browser instance in selenium using Java - java

How to change browser instance in Selenium- Java? WebDriver could not locate the elements on the screen after clicking on second webpage

// First store the current window instance
String winHandleBefore = driver.getWindowHandle();
// After your click operation which opens the new window
// Below code Switches instance to the new window
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
// If you want to switch back to the original window
driver.switchTo().window(winHandleBefore);

Related

Selenium Testing - Text addition via pop up window is not getting saved

I am trying to automate a website using selenium web driver.
On the website, there is a popup form for creating a gate name with a text field to enter the text. Using selenium I tried to do, the pop up came but the given text was not getting saved. To pop up it's working correctly. Need guides to complete the action.
Methods I used are given below:
Alert alert=driver.switchTo().alert();
driver.switchTo().alert().sendKeys("New Gate");
alert.accept();
System.out.println(alert.getText());
Elements are identified by the Xpath element locator.
I also gave text along with URL, another method that I got from the stack.
If its an alert then right way handle as per your requirement steps should be
switch to and alert
enter text
get entered text
accept
Alert alert=driver.switchTo().alert();
alert.sendKeys("New Gate");
System.out.println(alert.getText());
alert.accept();`
If its a model popup then try to locate popup element and directly used send keys
sample code :
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.your_popup_locator));
driver.findElement(By.you_textbox_locator).sendKeys("expected text");
You need to switch to pop-up first. Then you can access elements in the pop-up.
// save your main window handle
String MainWindow=driver.getWindowHandle();
// Get all window handle
Set<String> handles = driver.getWindowHandles();
Now, for each childHandle in handles,
driver.switchTo().window(childHandle );
// check if your textField present.
// If found, do your actions and break;
// else switch to next window
At last, switch to main window again.
driver.switchTo().window(MainWindow);

unable to locate element in new tab chrome - Selenium Webdriver with java

When clicking the menu in the application, it automatically opens that page in a new tab, then do the action in the new tab
String mainWindow = driver.getWindowHandle();
//when click the below its opened in new tab
driver.findElement(By.cssSelector("span.slds-align-middle")).click();
Set<String> handles = driver.getWindowHandles();
for (String handle : handles) {
if (!handle.equals(mainWindow)) {
driver.switchTo().window(handle);
break;
}
}
driver.findElement(By.id("23:311;a")).click();
After switching to the new window unable to locate the element
driver.findElement(By.id("23:311;a"))
How can I click the element in the newly opened tab?
HTML:
This is salesforce application
<span class="slds-icon_container slds-icon-utility-new-window slds-m-left--x-small focus-icon" data-aura-rendered-by="54:375;a">
23:311;a is not ID attribute, it data-aura-rendered-by attribute and it seems to be random so you can't really relay on that to locate the element. I suggest you use unique class name. lds-icon-utility-new-window seems unique enough
driver.findElement(By.className("lds-icon-utility-new-window"));
Or combination of several class attributes
driver.findElement(By.cssSelector(".slds-icon_container.slds-icon-utility-new-window"));

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

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.

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