简体   繁体   中英

Not able to switch to pop-up window and find any elements in pop-up in Webdriver using Java

I have an application in which i tried clicking on a button and in return it will pop-up a window for filling a new user form. This is not really like a pop-up window, because it has some input fields as well as "save " and "cancel" button. It looks similar to pop-up window in facebook.

Here the code i tried with

Set beforePopup = driver.getWindowHandles();
    driver.findElement(By.xpath("/html/body/div/div/div/div[3]/div/div[2]/div[2]/table/tbody/tr/td[3]/table/tbody/tr/td[2]/em/button")).click();
    driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
    Set afterPopup = driver.getWindowHandles();
    afterPopup.removeAll(beforePopup);
    if(afterPopup.size() == 1) {
          driver.switchTo().window((String)afterPopup.toArray()[0]);
        }

    //driver.switchTo().window("Add New User");
    //selenium.type("userDetailsBean.firstName","alan1");
    //WebElement btnStartQueryInside = driver.findElement(By.xpath("//html/body/div[14]/div/div/div/div/span"));
    //btnStartQueryInside.getText();
    System.out.println(driver.getTitle());
    WebElement firstName = driver.findElement(By.xpath("/html/body/div[2]/div/div/div/div/div/form/div/div/input"));
    firstName.sendKeys("alan1");



    //driver.switchTo().alert();
    //driver.findElement(By.xpath("//html/body/div[14]/div/div/div/div")).getText();
    //WebElement firstName=driver.findElement(By.xpath("/html/body/div[2]/div/div/div/div/div/form/div/div/input"));
    //firstName.sendKeys("alan1");
    /*WebElement lastName=driver.findElement(By.id("userDetailsBean.lastName"));
    lastName.sendKeys("harper");
    WebElement emailadd=driver.findElement(By.id("userDetailsBean.userEmail"));
    emailadd.sendKeys("alan1@derik.com");
    WebElement username=driver.findElement(By.id("userDetailsBean.userName"));
    username.sendKeys("xalan1");
    WebElement password=driver.findElement(By.id("adminPassword"));
    password.sendKeys("Derik123");
    WebElement repassword=driver.findElement(By.id("adminPassword2"));
    repassword.sendKeys("Derik123");
    driver.findElement(By.xpath("//html/body/div[2]/div/div/div/div/div[2]/div/div/table/tbody/tr/td/table/tbody/tr/td[2]/em/button")).click();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);*/

Please note that in code I commented some input field filling, because I thought first I will make it working just for 1st field. Please help me how to proceed.

the problem is after clicking the pop-up button, I'm not sure if the control switches to pop-up window or not. the gettitle after pop-up gives the main window title. and it is not able to find the first input field using the xpath or id.

The term window in Selenium means the actual browser window, not frames or just divs. Therefore, your logic is wrong, getWindowHandles() and driver.switchTo().window("Add New User") are not what you are after.

What you need is driver.switchTo().frame() .

driver.switchTo().defaultContent(); // optional, use only if driver is already in an iframe

WebElement editUserForm = driver.findElement(By.cssSelector("iframe[src*='editUserForm']"));
// there are other overloads (by frame name, index, id) and locators can be used here.
driver.switchTo().frame(editUserForm);

// make sure your locator here is correct
WebElement lastName = driver.findElement(By.id("userDetailsBean.lastName")); // I doubt this is correct
// from your screenshot, I'd suggest By.cssSelector("[id*='userDetailsBean.lastName'] input")
lastName.sendKeys("harper");

Also just a kindly heads up, your code smells.

  • Your implicitlyWait wait usage seems wrong, please read the documentation and the post .

  • Where did you get selenium.type("userDetailsBean.firstName","alan1"); ? Did you just copy the line from somewhere else? This looks like Selenium RC to me.

  • Why driver.switchTo().alert() ? Have you read the documentation on what's this for?
  • Please don't use absolute xpath.
  • Try use page object if you haven't done that in your real project. (It's fine if you just want to illustrate the problem here)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM