selenium 如何从同一窗口中的弹出窗口中获取xpath?

iovurdzv  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(319)

我需要发送值到这个弹出窗口。这是在同一个窗口中打开的。当我尝试与正常的驱动程序。findelement,它的获取错误消息说xpath找不到这是我的代码

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Homepage_login {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        FirefoxDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        //driver.get("http://se.rdmsonline.net/login.aspx");

        driver.get("http://se.rdmsonline.net/");
        driver.findElement(By.xpath("//*[@id='rcmLang_Arrow']")).click();

        driver.findElement(By.xpath("//*[@id='rcmLang_DropDown']/div/ul/li[2]")).click();

        driver.findElement(By.xpath("//*[@id='txtUserName']")).sendKeys(" ");

        driver.findElement(By.xpath("//*[@id='txtPassword']")).sendKeys(" ");

        driver.findElement(By.xpath("//*[@id='btnLogin']")).click();

        driver.findElement(By.xpath("//*[@id='Div1']/div/a")).click();

        driver.findElement(By.xpath("//*[@id='txtUserName']")).sendKeys(" ");

        driver.findElement(By.xpath("//*[@id='txtPassword']")).sendKeys(" ");

        driver.findElement(By.xpath("//*[@id='btnLogin']")).click();

        driver.findElement(By.xpath("//*[@id='rptSec_ctl00_imb']")).click();

        driver.findElement(By.xpath("//*[@id='ctl00_Menu5']")).click();

        driver.findElement(By.xpath("//*[@id='ctl00_cpl1_lnkNewNotification']")).click();

        driver.findElement(By.xpath("//*[@id='ctl00_cplm_dtpDate_dateInput_text']")).sendKeys(" ");


    }

}

这是发送弹出窗口内的控制..有人能帮助我吗?

e4eetjau

e4eetjau1#

首先,您应该切换到弹出窗口,然后尝试查找元素。
您可以使用以下代码片段

String parentWindow = driver.getWindowHandle();
Set<String> windowHandles = driver.getWindowHandles();
Iterator<String> iterator = windowHandles.iterator();
while (iterator.hasNext()) {
   String handle = iterator.next();
   if (!handle.contains(parentWindow)) {
                    // Switch to popup 
                    driver.switchTo().window(handle);
                    // Add code to find element 
       }
}
// Switch back to original window
driver.switchTo().window(parentWindow);

此代码可用于任意数量的弹出窗口

相关问题