[英]Not able to switch to pop-up window and find any elements in pop-up in Webdriver using Java
我有一個應用程序,嘗試在其中單擊一個按鈕,作為回報,它將彈出一個窗口,用於填寫新的用戶表單。 這實際上不像是彈出窗口,因為它具有一些輸入字段以及“保存”和“取消”按鈕。 它看起來類似於facebook中的彈出窗口。
這是我嘗試過的代碼
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);*/
請注意,在代碼中我注釋了一些輸入字段填充,因為我認為首先我將使其僅在第一個字段上起作用。 請幫助我如何進行。
問題是單擊彈出按鈕后,我不確定控件是否切換到彈出窗口。 彈出窗口后的gettitle提供主窗口標題。 並且無法使用xpath或id查找第一個輸入字段。
Selenium中的window
一詞是指實際的瀏覽器窗口,而不是框架或div。 因此,您的邏輯是錯誤的, getWindowHandles()
和driver.switchTo().window("Add New User")
不是您想要的。
您需要的是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");
同樣,請注意,您的代碼有味道。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.