Handling dynamically changing element id - using selenium webdriver - java

I m having the login page, when click login button it opens the new tab.
i moved the control to new window using,
driver.switchTo().window("_blank");
When I click one Button it will open the new popup (that popup is not a normal window it is a iframe).
I have selected a popup window using,
driver.switchTo().frame("frameName");
That popup has the list of records; each record has the separate "select" option (with the dynamically changing id's).
driver.findElement(By.xpath("//a[#id='radgrdPeople_ctl00_ctl04_lnkSelect']")).click();
When selecting the record from list, the popup window will be closed and the page will get refreshed.
Now I want to return the control to my parent window for doing some other stuff.
The record got selected successfully. But i could not able to focus the parent window again.
I have tried:
driver.switchTo().defaultcontent();
driver.switchTo().window("_blank");
And
driver.getWindowHandles()
Still I m getting the same problem.
I don't know whether I have to use any java script executor for handling the dynamically changing element id's.
Could u any one please help me on this....
Thanks in Advance.
By
K.Ranjithkumar

In the post after clicking the login button you used WindowName to switch over to the new window. But, in the tried solution, you used empty string to switch over to the new window.

You should make first window as parent window
String parent=driver.getwindowhandle;
// then, set String popup to be equal to window handle of the
// popup window
driver.switchTo().window( popup);
driver.findElement(By.id("okbutton")).click(); //assumes close of popup
Now after performing tasks on popup window, return control to parent window:
driver.switchTo().window( parent );// switch back to parent window
driver.switchTo().defaultcontent(); // reset iframe context

Related

Selenium WebDriver code cannot track elements in the window, having no Title or URL

I am on a page. After clicking on a link, a new window is opening. The window does not have any URL or title. So, while switching to the new window, aftre I am inspecting element, &, creating Xpath, Selenium code can't focus on the new window, and is not executing anything after that. Can you please resolve my query?
Note: After I inspect, the console window title is showing the URL of the parent window.
You can switch between different open windows you have opened:
String storeCurrentWindowHandler = driver.getWindowHandle();
Then perform the click operation that opens a new window
Make a switch to the new window that has opened
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
Do whatever the test needs within the selected window
Then close the new window, if it is no longer required
driver.close();
And switch back to the first window that had been opened
driver.switchTo().window(storeCurrentWindowHandler);
Hopefully this will help you to solve your issue.

Selenium Web Driver multiple windows

Scenario is
IN gmail email popup out if i am clicking on send button Email will send that popup will close automatically once i click on send button
My question is
if child window close automatically after how to switch to parent window?
You can do this by getting a handle to the parent window before you switch to the new window. Then after the new window closes you can switch back to the parent using this handle.
// before opening the new window
String parentWindowHandle = driver.getWindowHandle();
// after closing the new window
driver.switchTo().window(parentWindowHandle);

Select an option from context menu using selenium, sendKeys doesn't work

I'm trying to select an option from context menu, and sendKeys(Keys.ARROW_DOWN) is not working. All it does is that it moves the scroll of the page up and down(even though context menu is still open)
Actions action = new Actions(driver);
action.contextClick(element).build().perform();
action.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.Enter).build().perform();
the sendKeys method here just moves the page up and down without taking the opened context menu into consideration.(I also tried switching to an alert)
Is there another way to select an option from the context menu?
try this code :
action.contextClick(element).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.Enter).build().perform();
To navigate the context menu, you have to press the shift key AND arrow keys. Like this:
actionChains.context_click(element).send_keys(Keys.SHIFT, Keys.ARROW_DOWN, 'H')
The thing I have trouble with though is how to navigate down more than one menu item. Mine just always stops at the second item

There is a popup, but driver.getWindowHandles() return me only one element which is main window/parent window

Here, I attach the screenshot of that page I'm working on automation. For authentication, a pop-up window is opened. To switch pop-up window, when I use driver.getWindowHandles() it return only one element(window) which is main/parent window. For this, I can't switch to the pop-up window and can't do any operation on the pop-up.

Alert Handling in Selenium Webdriver

I tried to switch to popup alert and click on OK button, but i got an error saying that xpath (for OK button) is not found.
But this is working for me sometimes using the same code. Could anyone help me out on this. I tried all possible ways that is available in blogs. But i couldn't make it
you need to move control to your pop-up window first before doing any operation on pop-up window:-
below code is to move selenium control on pop-up window
driver.switchTo().alert();
by writing below line
alert.accept();
alert will get close
Based on the original question and the subsequent comments, I suspect what you're dealing with is a browser pop up window and not an alert. So this won't work
driver.switchTo().alert().accept();
You need to use window handles
Set<String> handles = driver.getWindowHandles();
Iterator<String> windows = handles.iterator();
String parent = windows.next();
String child = windows.next();
driver.switchTo().window(child);
driver.findElement(By.xpath("insert xpath to OK button")).click();
driver.switchTo().window(parent);
//continue with steps on parent window
Note: ensure that you add the required synchronization to the code snippet above

Categories