NoSuchElementException:无法在弹出窗口中找到元素

时间:2019-01-10 07:40:20

标签: java selenium selenium-webdriver

我已经切换到弹出窗口,因为可以单击弹出窗口中的按钮,但是我无法找到单选按钮元素。

这是我的代码:

driver.findElement(By.id("searchPrimIndustryImage")).click();

String mainWindow = driver.getWindowHandle();

    Set<String> s1 = driver.getWindowHandles();
    Iterator<String> i1 = s1.iterator();
    while(i1.hasNext())
    {
        String popupWindow = i1.next();
        if(!mainWindow.equalsIgnoreCase(popupWindow))
        {
            driver.switchTo().window(popupWindow);
            driver.findElement(By.id("Image1")).click();
            driver.findElement(By.xpath("//input[@value='28049']")).click();
            driver.findElement(By.id("Image5")).click();
        } 
    }

单选按钮元素是具有xpath的元素。

1 个答案:

答案 0 :(得分:1)

如果您确定xpath正确并且该元素确实存在于页面上,请尝试使用等待。在单击xpath元素之前,尝试添加以下内容:

WebDriverWait wait = new WebDriverWait(driver,30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@value='28049']")));

您将需要导入

import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;