简体   繁体   English

无法使用Java切换到弹出窗口并在Webdriver中找到弹出窗口中的任何元素

[英]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. 它看起来类似于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. 弹出窗口后的gettitle提供主窗口标题。 and it is not able to find the first input field using the xpath or id. 并且无法使用xpath或id查找第一个输入字段。

The term window in Selenium means the actual browser window, not frames or just divs. Selenium中的window一词是指实际的浏览器窗口,而不是框架或div。 Therefore, your logic is wrong, getWindowHandles() and driver.switchTo().window("Add New User") are not what you are after. 因此,您的逻辑是错误的, getWindowHandles()driver.switchTo().window("Add New User")不是您想要的。

What you need is driver.switchTo().frame() . 您需要的是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"); 您从哪里获得selenium.type("userDetailsBean.firstName","alan1"); ? Did you just copy the line from somewhere else? 您只是从其他地方复制行吗? This looks like Selenium RC to me. 对我来说,这看起来像Selenium RC。

  • Why driver.switchTo().alert() ? 为什么选择driver.switchTo().alert() Have you read the documentation on what's this for? 您是否阅读有关此用途的文档
  • Please don't use absolute xpath. 请不要使用绝对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) (如果您只想在此处说明问题,那很好)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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