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"));
Related
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);
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 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);
I have used selenium 2.31.
I have used Actions class for mouse movement. Using this I moved the mouse over a menu and its submenu appeared only for a fraction of second unlike with older version of Firefox .
Because of this issue I cannot select the sub menu using driver.findElement as it throws an exception "element cannot be scrolled into view".
Is there any solution for this?
With the actions object you should first move the menu title, and then move to the popup menu item and click it. Don't forget to call actions.perform() at the end. Here's some sample Java code:
Actions actions = new Actions(driver);
WebElement menuHoverLink = driver.findElement(By.linkText("Menu heading"));
actions.moveToElement(menuHoverLink);
WebElement subLink = driver.findElement(By.cssSelector("#headerMenu .subLink"));
actions.moveToElement(subLink);
actions.click();
actions.perform();
Another way to go about this is to use Selenium's JavaScript Executor to force the style of the element to be displayed.
An Example of this would be along this lines in C#
//Use the Browser to change the display of the element to be shown
(IJavaScriptExecutor)driver).ExecuteScript("document.getElementById('myId').stlye.display="block");
//navigate to your link that is now viewable
driver.FindElement(By.Xpath('//LinkPath')).Click();
From there, you can find the XPath to your element and use selenium to click on the element. You can cascade this to find children of your main element as well
//(IJavaScriptExecutor)ffbrowser).ExecuteScript("document.getElementById('myId').children[1].children[1].style.display='block'");
Note that this is only possible if you have a hover element that changes the display style when hovered over.
Try this code...
It's c sharp code...
//Webelement is the main menu Link
webElement = driver.FindElement(By.XPath("Your element xpath"));
Actions act = new Actions(driver);
act.MoveToElement(webElement).Perform();//This opens menu list
System.Threading.Thread.Sleep(5000);//This line will help you to hold menu
//This web element is the sub menu which is under main menu
webElement = driver.FindElement(By.XPath("Sub menu path"));
act.MoveToElement(webElement).Perform();//This opens menu list
System.Threading.Thread.Sleep(5000);//Holds menu
//This web element is the option you have to click
webElement = driver.FindElement(By.XPath("Path"));
webElement.Click();
This will be helpful if you are using Ruby.
1.First you need to find element by xpath or id.
2.Then use the method action.move_to().perform.
Here is the code:
hover = WAIT.until{$driver.find_element(:xpath,"xpath")}
driver.action.move_to(hover).perform
This answer helped solve my problem.
My challange was to find a link under a menu option.
The link was not visible until I hovered over the Menu.
This crucial part for me was finding out that in addition to hovering over the menu, I next had to hover on the link in order to interact with it.
List<WebElement> list = driver.findElements(By.xpath("//a"));
for (int i=0;i<list.size();i++){
if(list.get(i).getText().equalsIgnoreCase("cacique intimates M"))
{
new Actions(driver).moveToElement(list.get(i)).click().build().perform();
System.out.println("Clicked on Parent Category");
new Actions(driver).moveToElement(list.get(i)).moveToElement(driver.findElement(By.linkText("SPECIALTY BRAS"))).click().build().perform();
break;
}
}
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);