首页 文章

org.openqa.selenium.NoSuchElementException:无法找到元素:

提问于
浏览
1

我无法单击按钮,因为无法在屏幕截图的标记行上找到元素和异常抛出而抛出异常 .

该按钮需要一些时间来加载,我已经将时间增加到100秒,但这并没有修复错误 .

码:

public static WebElement viewShipment(WebDriver driver, String mskuType) {
    WebElement noOfShipment = driver.findElement(By.xpath(".//*[@id='fba-core-workflow-shipment-summary-shipment']"));
    WebDriverWait wait = new WebDriverWait(driver, 15);
    List<WebElement> shipmentList = noOfShipment.findElements(By.tagName("tr"));
    int shipmentCount = shipmentList.size();

    for (int row=1;row<shipmentCount;row=+1)
    {
        WebElement onOfSkuWE= driver.findElement(By.xpath(".//*[@id='fba-core-workflow-shipment-summary-shipment']/tr["+row+"]/td[3]"));
        String noOfSku = onOfSkuWE.getText();
        int noOfSkuValue = Integer.parseInt(noOfSku);
        for(int i=0;i<2;i++)
        {
            try{
                if(mskuType.equalsIgnoreCase("single"))
                {
                    if(noOfSku.equalsIgnoreCase("1"))
                    {
                        Thread.sleep(10000);
                        WebElement workOnShipmentWE = driver.findElement(By.xpath(".//*[@id='fba-core-workflow-shipment-summary-shipment']/tr["+row+"]/td[6]/button"));
                        wait.until(ExpectedConditions.visibilityOf(workOnShipmentWE));
                        workOnShipmentWE.click();
                        break;
                    }
                }
                else if(mskuType.equalsIgnoreCase("multiple"))
                {
                    if(noOfSkuValue>1)
                    {
                        WebElement moreThanOneUnit = driver.findElement(By.xpath(".//*[@id='fba-core-workflow-shipment-summary-shipment']/tr["+row+"]/td[6]/button"));
                        wait.until(ExpectedConditions.elementToBeClickable(moreThanOneUnit));
                        moreThanOneUnit.click();
                        break;
                    }
                }
            }
            catch(Exception e)
            {
                driver.navigate().refresh();

                e.getMessage();
            }
        }
    }
    return element;
}

HTML:

<tbody id="fba-core-workflow-shipment-summary-shipment">
    <tr>
        <td>FBA (11/3/16 9:32 PM) - 1</td>
        <td>FBA43K62MB</td>
        <td class="number total-line-items">1</td>
        <td class="number total-quantity">3</td>
        <td>
        <td>
            <button class="amznBtn btn-lg-pri-arrowr" onclick="window.location='#FBA43K62MB/prepare'" style="width: 28ex;" type="button" name="Work on shipment">
                <span>Work on shipment</span>
            </button>
            <p class="action-links content-bottom">
            <p class="action-links">
            <p/>
        </td>
    </tr>
    <tr style="display: none;">
    <tr>
    <tr style="display: none;">
    <tr>
    <tr style="display: none;">
</tbody>

1 回答

  • 0

    1.如果页面正在加载 too slow ,请尝试使用以下ExpectedCondition:

    WebElement myDynamicElement = (new WebDriverWait(driver, **10**)).until(**ExpectedConditions.presenceOfElementLocated**(By.id("fba-core-workflow-shipment-summary-shipment")));
    

    将代码保留为 viewShipment 方法的第一个语句 . 这将确保Web驱动程序等待元素(在本例中由Id指定)加载配置的时间(代码中为10秒) . Increase this value if page loading is too slow, as per your requirements.

    在Thread.sleep上使用ExpectedCondition的 advantage 是,每当找到一个元素时,它将立即返回(无需等待完成10秒,例如在Thread.sleep的情况下) .

    在抛出TimeoutException之前等待最多10秒,或者如果它发现元素将在0-10秒内返回它 . 默认情况下,WebDriverWait每500毫秒调用一次ExpectedCondition,直到成功返回 . ExpectedCondition函数类型的成功返回值是布尔值true或非null对象 .

    • If the element to found is, part of an iframe or new window: 如果元素在框架内(检查您要查找的元素是否是iframe标记的子元素),则首先找到该框架,然后切换到该框架 . 然后使用XPath查找元素 .

    如果元素在新窗口中(如单击链接时在新选项卡中打开),则先找到窗口然后切换到该窗口,然后尝试查找该元素 .

    参考文献:

相关问题