Selenium: unable to switch window - java

I am trying to switch to another pop-up window,but is giving some sort of error. I am not able to figure out why the code in not working for switching the window
Error it is giving is
java.lang.NullPointerException: null value in entry: name=null
Here is the code....
String parentWindowHandle = driver.getWindowHandle();
System.out.println("parentWindowHandle......"+parentWindowHandle);
//click on "register"
Actions act = new Actions(driver);
act.click(driver.findElement(By.id("ctl00_ContentPlaceHolder1_btnRegisterMe")));
act.perform();
sleep(26000);
String popUpWindowHandle=null;
Set<String> openWindowsList = driver.getWindowHandles();
for (String windowHandle : openWindowsList) {
System.out.println("windowHandle......"+windowHandle);
if (!windowHandle.equals(parentWindowHandle))
{popUpWindowHandle = windowHandle;
break;}
}
// Switching control to newly opened window
driver.switchTo().window(popUpWindowHandle);

If you are able to print window handles then try
if (!windowHandle.equalsIgnoreCase(parentWindowHandle)){
popUpWindowHandle = windowHandle;
break;
}
instead of
if(!windowHandle.equals(parentWindowHandle)){}

Related

How to switch focus with Selenium to a new pop-up Window with the SAME handle?

I am trying to change my driver's focus to the new window that pops up as a result of clicking a link. The only problem is that both the parent window and the pop-up window have the same handle - for example - the title of both is " <Company Name - Abbreviation 1>
How would I use Selenium JAVA to get focus on the pop-up window a different way than what I have used?
`
String originalWindow = driver.getWindowHandle();
assert driver.getWindowHandles().size() == 1; driver.findElement(By.xpath("xpath goes here")).click(); // Training
// Loop through until we find a new window handle
for (String windowHandle : driver.getWindowHandles()) {
if (!originalWindow.contentEquals(windowHandle)) {
driver.switchTo().window(windowHandle);
driver.manage().window().maximize();
}
}
`
Thanks, Everyone!
`
String originalWindow = driver.getWindowHandle();
assert driver.getWindowHandles().size() == 1;
driver.findElement(By.xpath("xpath goes here")).click(); // Training
Thread.sleep(1000);
// Loop through until we find a new window handle
for (String windowHandle : driver.getWindowHandles()) {
if (!originalWindow.contentEquals(windowHandle)) {
driver.switchTo().window(windowHandle);
driver.manage().window().maximize();
}
}
`
I tried the above code, but both windows sadly have the same title. I want to switch focus to the new window so I can then interact with the new one.

Calling href value in Selenium Webdriver

Could you please help me to call href value which is changing every time. Below is the code for your reference:
click here
Thanks.
As I understand from your comment, Test cases needed the href (link) attribute value. So code can be written by this way :
String strLinkHref = driver.findElement(By.linkText("click here")).getAttribute("href");
or
String strLinkHref = driver.findElement(By.xpath("//a[text()='click here']")).getAttribute("href");
Note : here you can store in String and print. It will get link dynamically every time.
If test case needed to open it, then you can use :
driver.get(strLinkHref);
If you require to move to TAB window, Please use below code :
String handle= driver.getWindowHandle();
System.out.println(handle);
// Click on the Button "New Message Window"
driver.findElement(By.name("New Message Window")).click();
// Store and Print the name of all the windows open
Set handles = driver.getWindowHandles();
System.out.println(handles);
// Pass a window handle to the other window
for (String handle1 : driver.getWindowHandles()) {
System.out.println(handle1);
driver.switchTo().window(handle1);
currentURL = driver.getCurrentUrl();
System.out.println(currentURL);
}
// Closing Pop Up window
driver.close();
Reference

how to select the drop down box in selenium web driver

Select se = new Select(driver.findElement(By.xpath(".//*[#id='33629']/div/div[1]/div[2]/div[1]/select")));
se.selectByIndex(7);
driver.findElement(By.xpath(".//*[#id='33629']/div/div[1]/div[2]/div[1]/select/option[8]")).click();
Above code doesn't work,please help
Error returned:
Exception in thread "main" org.openqa.selenium.NoSuchWindowException: no such window: target window already closed from unknown error: web view not found
org.openqa.selenium.NoSuchWindowException: no such window
Means the browser is close when you are trying to interact with it. Remove driver.close() from your code and put it only after you have finished all you interactions with the browser.
Edit
If you need to return to parent window after closing child window use driver.switchTo() again
// get parent window ID
String parentHandle = driver.getWindowHandle();
// switch to the new window
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(parentHandle))
{
driver.switchTo().window(handle);
}
}
//do something with the new window
// switch back to the old window
driver.close();
driver.switchTo().window(parentHandle);
windowIdbefore = driver.getWindowHandle();
System.out.println(windowIdbefore);
Set<String> windowid = driver.getWindowHandles();
for (String string : windowid) {
System.out.println(string);
driver.switchTo().window(string);
// enter code here
}
WebDriver driver=new FirefoxDriver();
Select s=new Select(driver.findElement(By.xpath("xpathExpression")));
s.selectByVisibleText("text");
s.selectByValue("value");
s.selectByIndex(1);
as i see here the dropdown box is present in div tag. i think with your code dropdown has been located but you are not able to select the value present in dropdown. Then follow below code
WebDriverWait wait = new WebDriverWait(d, 10);
Actions builder = new Actions(d);
WebElement selectvalue = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("your drop down xpath value")));
builder.mouse.mouseMove(((Locatable)selectvalue).coordinates);
selectvalue.click();
WebElement option = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("locator value of dropdown value(your dropdown value)")));
builder.mouse.mouseMove(((Locatable)option).coordinates);
option.click();
System.out.println("dropdown value slected...");

Unable to Click using click method (No Exception) as well as not able to switch to parent window

Here is my config:
Windows 7 VM + Selenium Web Driver + IE 11 + IE Driver Server 64 bit
I have a weird problem, where in, when a click event is encountered in code, the execution just suspends and doesn't proceed. I was able to overcome this by using sendKeys(Keys.Enter) instead of click. But i see the same behavior while trying to switch to a popup window and returning back to the parent frame.
This is the code i use for switching to child and returning to parent window :
WebDriver popup = null;
String popwin = "";
boolean present;
try {
System.out.println(driver.getCurrentUrl());
String winHandleBefore = driver.getWindowHandle();
for (String winHandle : driver.getWindowHandles()) {
System.out.println("WinHandle : " + winHandle);
popwin = winHandle;
}
popup = driver.switchTo().window(popwin);
Actions action = new Actions(popup); action.moveToElement(popup.findElement(by.id("idContinue")))
.sendKeys(Keys.ENTER).build().perform(); //works fine till above, clicks continue button in popup and the popup closes automatically
driver.switchTo().window(winHandleBefore);
The execution suspends at the last line, the Thread keeps running with no results and no exceptions are thrown.
Thank you!!
Note: The application under test is an IE only compatible system and hence there is no way to test it in other browsers.(the App doesn't even load in other browsers)
Is this part of the code executed after the popup is displayed on the screen or before it?
If this is executed after the popup is shown then the line :
String winHandleBefore = driver.getWindowHandle();
is getting the instance of the popup, and on closing the pop up by clicking the button you are destroying that instance itself.
So there are 2 solutions to this issues:
Place the line : [String winHandleBefore = driver.getWindowHandle();] before the popup appears on the screen.
Modify the present code:
WebDriver popup = null;
String popwin = "";
boolean present;
try {
System.out.println(driver.getCurrentUrl());
for (String winHandle : driver.getWindowHandles()) {
System.out.println("WinHandle : " + winHandle);
driver.switchTo().window(winHandle);
Actions action = new Actions(popup);
action.moveToElement(popup.findElement(by.id("idContinue"))).sendKeys(Keys.ENTER).build().perform();
break;
}
These two tricks will surely solve the issue you are facing

How to pass values from one page to another page in Firefox using Selenium WebDriver?

I have a registration page that contains many input fields. This registration page will open when I click on a Hyperlink i.e. registration link, it will open a new tab and then I need to put the values for the respective input fields such as Email Address, First Name, Last Name and so forth through the script (selenium webdriver - java).
As of now, it opens a new window with the registration page but its not placing the values in the respective fields..
I have used the following script :
webDriver.findElement(By.name("email")).sendKeys("paul#vendormate.vm");
webDriver.findElement(By.id("vmBtnSubmitExpReg")).click(); webDriver.findElement(By.name("confirmEmail")).sendKeys("paul#vendormate.vm");
Can anyone tell me what shall I do to place the values in the registration page?
Thanks
You need to change window for webdriver
String currentWindow = driver.getWindowHandle(); //saves current window name
//opens new window
String popupWindow = webDriverExtensions.popupHandle(currentWindow); //now you have second window name
driver.switchTo().window(popupWindow); // switched to registration window
// doing your job
driver.close(); //closing windows
driver.switchTo().window(currentWindow); //return back to main window
public String popupHandle(String existingWindowHandle) {
String popupHandle = null;
Set<String> windowHandles = driver.getWindowHandles();
for (String handle : windowHandles) {
if (!handle.equals(existingWindowHandle)) {
popupHandle = handle;
break;
}
}
return popupHandle;
}

Categories

Resources