Reputation: 513
I want to handle sign-in part in rediff.com, but the below code doesn't work for that:
driver.get("http://www.rediff.com/");
WebElement sign = driver.findElement(By.xpath("//html/body/div[3]/div[3]/span[4]/span/a"));
sign.click();
String myWindowHandle = driver.getWindowHandle();
driver.switchTo().window(myWindowHandle);
WebElement email_id= driver.findElement(By.xpath("//*[@id='signin_info']/a[1]"));
email_id.sendKeys("hi");
If myWindowHandle
is not the correct string, then let me know how to get the pop-up Window name, because I can't find the name of the pop-up window.
Upvotes: 25
Views: 293951
Reputation: 11
from selenium import webdriver
driver = webdriver.Chrome()
driver.get(url)
# Save a list of all open window handles
window_handles = driver.window_handles
# Open new window with click() action - modify as needed.
get_element(driver, By.XPATH, "//input[@title]").click()
# Get and switch to the pop-up window
new_popup_window = list(set(driver.window_handles) - set(window_handles))[0]
driver.switch_to.window(new_popup_window)
Upvotes: 0
Reputation: 1
String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
String subWindowHandler = null;
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
subWindowHandler = iterator.next();
driver.switchTo().window(subWindowHandler); // switch to popup window
// Now you are in the popup window, perform necessary actions here
driver.switchTo().window(parentWindowHandler); // switch back to parent window
Upvotes: 0
Reputation: 3630
To switch to a popup window, you need to use getWindowHandles()
and iterate through them.
In your code you are using getWindowHandle()
which will give you the parent window itself.
String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
String subWindowHandler = null;
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler); // switch to popup window
// Now you are in the popup window, perform necessary actions here
driver.switchTo().window(parentWindowHandler); // switch back to parent window
Upvotes: 45
Reputation: 51
You can use the below code inside your code when you get any web browser pop-up alert message box.
// Accepts (Click on OK) Chrome Alert Browser for RESET button.
Alert alertOK = driver.switchTo().alert();
alertOK.accept();
//Rejects (Click on Cancel) Chrome Browser Alert for RESET button.
Alert alertCancel = driver.switchTo().alert();
alertCancel.dismiss();
Upvotes: 5
Reputation: 1
public void Test(){
WebElement sign = fc.findElement(By.xpath(".//*[@id='login-scroll']/a"));
sign.click();
WebElement LoginAsGuest=fc.findElement(By.xpath(".//*[@id='guest-login-option']"));
LoginAsGuest.click();
WebElement email_id= fc.findElement(By.xpath(".//*[@id='guestemail']"));
email_id.sendKeys("[email protected]");
WebElement ContinueButton=fc.findElement(By.xpath(".//*[@id='contibutton']"));
ContinueButton.click();
}
Upvotes: -1
Reputation: 1
When the toastr message poped up on the screen of firefox. the below tag was displayed in fire bug.
<div class="toast-message">Invalid Credentials, Please check Password</div>.
I took the screenshot at that time. And did the below changes in selenium java code.
String alertText = "";
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("toast-message")));
WebElement toast1 = driver.findElement(By.className("toast-message"));
alertText = toast1.getText();
System.out.println( alertText);
And my issue of toastr popup got resolved.
Upvotes: 0
Reputation: 125
You can handle popup window or alert box:
Alert alert = driver.switchTo().alert();
alert.accept();
You can also decline the alert box:
Alert alert = driver.switchTo().alert();
alert().dismiss();
Upvotes: 5
Reputation: 49
//get the main handle and remove it
//whatever remains is the child pop up window handle
String mainHandle = driver.getWindowHandle();
Set<String> allHandles = driver.getWindowHandles();
Iterator<String> iter = allHandles.iterator();
allHandles.remove(mainHandle);
String childHandle=iter.next();
Upvotes: -1
Reputation: 2337
Do not make the situation complex. Use ID
if they are available.
driver.get("http://www.rediff.com");
WebElement sign = driver.findElement(By.linkText("Sign in"));
sign.click();
WebElement email_id= driver.findElement(By.id("c_uname"));
email_id.sendKeys("hi");
Upvotes: 2
Reputation: 513
I found the solution for the above program, which had the goal of signing in to http://rediff.com
public class Handle_popupNAlert
{
public static void main(String[] args ) throws InterruptedException
{
WebDriver driver= new FirefoxDriver();
driver.get("http://www.rediff.com/");
WebElement sign = driver.findElement(By.xpath("//html/body/div[3]/div[3]/span[4]/span/a"));
sign.click();
Set<String> windowId = driver.getWindowHandles(); // get window id of current window
Iterator<String> itererator = windowId.iterator();
String mainWinID = itererator.next();
String newAdwinID = itererator.next();
driver.switchTo().window(newAdwinID);
System.out.println(driver.getTitle());
Thread.sleep(3000);
driver.close();
driver.switchTo().window(mainWinID);
System.out.println(driver.getTitle());
Thread.sleep(2000);
WebElement email_id= driver.findElement(By.xpath("//*[@id='c_uname']"));
email_id.sendKeys("hi");
Thread.sleep(5000);
driver.close();
driver.quit();
}
}
Upvotes: 11