找不到存在的元素,获取NullPointerException而不是NoSuchElement

问题描述 投票:0回答:2

我正在尝试自动化网络数据表测试。我在我用来定位的每个WebElement对象中得到这个NullPointerException。我的代码尽可能简单:

public class tableTestStepDefs {

@FindBy(how = How.XPATH, using = "//*[@id=\"contenido-table\"]/tbody/tr[1]/td[3]")
public WebElement cell1;
@FindBy(how = How.ID, using = "\"DTE_Field_sufijo\"")
public WebElement editableCell1;

@When("^i click on cell1 to make it editable$")
public void hagoClickEnUnaCeldaDeLaColumnaSufijo(){
driver.get("http://192.168.242.104:7777/some/context");
cell1.click();
}

我在每个对象中都得到NullPointerException,而不是NoSuchElementException,当驱动程序无法找到某个元素时,我通常会得到它,而驱动程序正在进入页面。如果我调试,我可以在驱动程序的实例中找到具有相同id或xpath表达式的所有元素。不知道发生了什么事

这是HTML部分:

<div class="dataTables_scrollBody ps-container ps-theme-default ps-active-x" data-ps-id="77fa0bd8-5ef4-1c4e-e67a-a8675b515900" style="position: relative; overflow: auto; width: 100%; height: 762px;">
    <table id="contenido-table" class="table table-condensed table-striped dataTable no-footer" role="grid" aria-describedby="contenido-table_info" style="width: 2072px;">
        <thead>
            <tr role="row" style="height: 0px;">
                <th class="text-center sorting" aria-controls="contenido-table" rowspan="1" colspan="1" style="width: 112px; padding-top: 0px; padding-bottom: 0px; border-top-width: 0px; border-bottom-width: 0px; height: 0px;" aria-label="ICAO Aerolinea: Activar para ordenar la columna de manera ascendente">
                    <div class="dataTables_sizing" style="height: 0px; overflow: hidden;">ICAO Aerolinea</div>
                </th>
                <th class="text-center sorting" aria-controls="contenido-table" rowspan="1" colspan="1" style="width: 43px; padding-top: 0px; padding-bottom: 0px; border-top-width: 0px; border-bottom-width: 0px; height: 0px;" aria-label="Vuelo: Activar para ordenar la columna de manera ascendente">
                    <div class="dataTables_sizing" style="height: 0px; overflow: hidden;">Vuelo</div>
                </th>
                <th class="text-center editable sorting" aria-controls="contenido-table" rowspan="1" colspan="1" style="width: 43px; padding-top: 0px; padding-bottom: 0px; border-top-width: 0px; border-bottom-width: 0px; height: 0px;" aria-label="Sufijo: Activar para ordenar la columna de manera ascendente">
                    <div class="dataTables_sizing" style="height: 0px; overflow: hidden;">Sufijo</div>
                </th>
                <th class="text-center sorting" aria-controls="contenido-table" rowspan="1" colspan="1" style="width: 71px; padding-top: 0px; padding-bottom: 0px; border-top-width: 0px; border-bottom-width: 0px; height: 0px;" aria-label="Matricula: Activar para ordenar la columna de manera ascendente">
                    <div class="dataTables_sizing" style="height: 0px; overflow: hidden;">Matricula</div>
                </th>
                <th class="text-center sorting" aria-controls="contenido-table" rowspan="1" colspan="1" style="width: 33px; padding-top: 0px; padding-bottom: 0px; border-top-width: 0px; border-bottom-width: 0px; height: 0px;" aria-label="Tipo: Activar para ordenar la columna de manera ascendente">
                    <div class="dataTables_sizing" style="height: 0px; overflow: hidden;">Tipo</div>
                </th>
                <th class="text-center sorting" aria-controls="contenido-table" rowspan="1" colspan="1" style="width: 72px; padding-top: 0px; padding-bottom: 0px; border-top-width: 0px; border-bottom-width: 0px; height: 0px;" aria-label="Aeronave: Activar para ordenar la columna de manera ascendente">
                    <div class="dataTables_sizing" style="height: 0px; overflow: hidden;">Aeronave</div>
                </th>
                <th class="text-center sorting" aria-controls="contenido-table" rowspan="1" colspan="1" style="width: 72px; padding-top: 0px; padding-bottom: 0px; border-top-width: 0px; border-bottom-width: 0px; height: 0px;" aria-label="Categoria: Activar para ordenar la columna de manera ascendente">
                    <div class="dataTables_sizing" style="height: 0px; overflow: hidden;">Categoria</div>
                </th>
                <th class="text-center sorting" aria-controls="contenido-table" rowspan="1" colspan="1" style="width: 52px; padding-top: 0px; padding-bottom: 0px; border-top-width: 0px; border-bottom-width: 0px; height: 0px;" aria-label="Origen: Activar para ordenar la columna de manera ascendente">
                    <div class="dataTables_sizing" style="height: 0px; overflow: hidden;">Origen</div>
                </th>
                <th class="text-center sorting" aria-controls="contenido-table" rowspan="1" colspan="1" style="width: 96px; padding-top: 0px; padding-bottom: 0px; border-top-width: 0px; border-bottom-width: 0px; height: 0px;" aria-label="Escala Previa: Activar para ordenar la columna de manera ascendente">
                    <div class="dataTables_sizing" style="height: 0px; overflow: hidden;">Escala Previa</div>
                </th>
                <th class="text-center sorting" aria-controls="contenido-table" rowspan="1" colspan="1" style="width: 96px; padding-top: 0px; padding-bottom: 0px; border-top-width: 0px; border-bottom-width: 0px; height: 0px;" aria-label="Estado Vuelo: Activar para ordenar la columna de manera ascendente">
                    <div class="dataTables_sizing" style="height: 0px; overflow: hidden;">Estado Vuelo</div>
                </th>
                <th class="text-center sorting" aria-controls="contenido-table" rowspan="1" colspan="1" style="width: 139px; padding-top: 0px; padding-bottom: 0px; border-top-width: 0px; border-bottom-width: 0px; height: 0px;" aria-label="Fecha Programada: Activar para ordenar la columna de manera ascendente">
                    <div class="dataTables_sizing" style="height: 0px; overflow: hidden;">Fecha Programada</div>
                </th>
                <th class="text-center sorting" aria-controls="contenido-table" rowspan="1" colspan="1" style="width: 116px; padding-top: 0px; padding-bottom: 0px; border-top-width: 0px; border-bottom-width: 0px; height: 0px;" aria-label="Fecha Estimada: Activar para ordenar la columna de manera ascendente">
                    <div class="dataTables_sizing" style="height: 0px; overflow: hidden;">Fecha Estimada</div>
                </th>
                <th class="text-center sorting" aria-controls="contenido-table" rowspan="1" colspan="1" style="width: 93px; padding-top: 0px; padding-bottom: 0px; border-top-width: 0px; border-bottom-width: 0px; height: 0px;" aria-label="Fecha Actual: Activar para ordenar la columna de manera ascendente">
                    <div class="dataTables_sizing" style="height: 0px; overflow: hidden;">Fecha Actual</div>
                </th>
                <th class="text-center sorting" aria-controls="contenido-table" rowspan="1" colspan="1" style="width: 58px; padding-top: 0px; padding-bottom: 0px; border-top-width: 0px; border-bottom-width: 0px; height: 0px;" aria-label="Servicio: Activar para ordenar la columna de manera ascendente">
                    <div class="dataTables_sizing" style="height: 0px; overflow: hidden;">Servicio</div>
                </th>
                <th class="text-center sorting" aria-controls="contenido-table" rowspan="1" colspan="1" style="width: 44px; padding-top: 0px; padding-bottom: 0px; border-top-width: 0px; border-bottom-width: 0px; height: 0px;" aria-label="Stand: Activar para ordenar la columna de manera ascendente">
                    <div class="dataTables_sizing" style="height: 0px; overflow: hidden;">Stand</div>
                </th>
                <th class="text-center sorting" aria-controls="contenido-table" rowspan="1" colspan="1" style="width: 47px; padding-top: 0px; padding-bottom: 0px; border-top-width: 0px; border-bottom-width: 0px; height: 0px;" aria-label="Cintas: Activar para ordenar la columna de manera ascendente">
                    <div class="dataTables_sizing" style="height: 0px; overflow: hidden;">Cintas</div>
                </th>
                <th class="text-center sorting" aria-controls="contenido-table" rowspan="1" colspan="1" style="width: 91px; padding-top: 0px; padding-bottom: 0px; border-top-width: 0px; border-bottom-width: 0px; height: 0px;" aria-label="Inicio Cintas: Activar para ordenar la columna de manera ascendente">
                    <div class="dataTables_sizing" style="height: 0px; overflow: hidden;">Inicio Cintas</div>
                </th>
                <th class="text-center sorting" aria-controls="contenido-table" rowspan="1" colspan="1" style="width: 73px; padding-top: 0px; padding-bottom: 0px; border-top-width: 0px; border-bottom-width: 0px; height: 0px;" aria-label="Fin Cintas: Activar para ordenar la columna de manera ascendente">
                    <div class="dataTables_sizing" style="height: 0px; overflow: hidden;">Fin Cintas</div>
                </th>
                <th class="text-center sorting" aria-controls="contenido-table" rowspan="1" colspan="1" style="width: 115px; padding-top: 0px; padding-bottom: 0px; border-top-width: 0px; border-bottom-width: 0px; height: 0px;" aria-label="Primera Maleta: Activar para ordenar la columna de manera ascendente">
                    <div class="dataTables_sizing" style="height: 0px; overflow: hidden;">Primera Maleta</div>
                </th>
                <th class="text-center sorting" aria-controls="contenido-table" rowspan="1" colspan="1" style="width: 107px; padding-top: 0px; padding-bottom: 0px; border-top-width: 0px; border-bottom-width: 0px; height: 0px;" aria-label="Ultima Maleta: Activar para ordenar la columna de manera ascendente">
                    <div class="dataTables_sizing" style="height: 0px; overflow: hidden;">Ultima Maleta</div>
                </th>
            </tr>
        </thead>
        <tbody>
            <tr role="row" class="odd">
                <td class=" text-center" name="ICAO Aerolinea">DRA</td>
                <td class=" text-center" name="Vuelo">5485</td>
                <td class=" text-center editable" name="Sufijo"></td>
                <td class=" text-center" name="Matricula" style="display: table-cell;text-overflow: ellipsis;overflow: hidden;max-width: 195px;">GBC001</td>
                <td class=" text-center" name="Tipo" style="display: table-cell;text-overflow: ellipsis;overflow: hidden;max-width: 195px;">NA</td>
                <td class=" text-center" name="Aeronave">A001</td>
                <td class=" text-center" name="Categoria">C</td>
                <td class=" text-center" name="Origen">LEBB</td>
                <td class=" text-center" name="Escala Previa" style="display: table-cell;text-overflow: ellipsis;overflow: hidden;max-width: 195px;"></td>
                <td class=" text-center" name="Estado Vuelo">SCH</td>
                <td class=" text-center" name="Fecha Programada" style="min-width: 97px">03/10/2018</td>
                <td class=" text-center" name="Fecha Estimada"></td>
                <td class=" text-center" name="Fecha Actual"></td>
                <td class=" text-center" name="Servicio">A</td>
                <td class=" text-center" name="Stand"></td>
                <td class=" text-center" name="Cintas"></td>
                <td class=" text-center" name="Inicio Cintas"></td>
                <td class=" text-center" name="Fin Cintas"></td>
                <td class=" text-center" name="Primera Maleta"></td>
                <td class=" text-center" name="Ultima Maleta"></td>
            </tr>
            <tr role="row" class="even">
                <td class=" text-center" name="ICAO Aerolinea">AAA</td>
                <td class=" text-center" name="Vuelo">1234</td>
                <td class=" text-center editable" name="Sufijo"></td>
                <td class=" text-center" name="Matricula" style="display: table-cell;text-overflow: ellipsis;overflow: hidden;max-width: 195px;">GBC001</td>
                <td class=" text-center" name="Tipo" style="display: table-cell;text-overflow: ellipsis;overflow: hidden;max-width: 195px;">NA</td>
                <td class=" text-center" name="Aeronave">A001</td>
                <td class=" text-center" name="Categoria">C</td>
                <td class=" text-center" name="Origen">LEBB</td>
                <td class=" text-center" name="Escala Previa" style="display: table-cell;text-overflow: ellipsis;overflow: hidden;max-width: 195px;"></td>
                <td class=" text-center" name="Estado Vuelo">SCH</td>
                <td class=" text-center" name="Fecha Programada" style="min-width: 97px">03/10/2018</td>
                <td class=" text-center" name="Fecha Estimada"></td>
                <td class=" text-center" name="Fecha Actual"></td>
                <td class=" text-center" name="Servicio">A</td>
                <td class=" text-center" name="Stand"></td>
                <td class=" text-center" name="Cintas"></td>
                <td class=" text-center" name="Inicio Cintas"></td>
                <td class=" text-center" name="Fin Cintas"></td>
                <td class=" text-center" name="Primera Maleta"></td>
                <td class=" text-center" name="Ultima Maleta"></td>
            </tr>
        </tbody>
    </table>
    <div class="ps-scrollbar-x-rail" style="width: 2063px; left: 0px; bottom: 3px;">
        <div class="ps-scrollbar-x" tabindex="0" style="left: 0px; width: 2055px;"></div>
    </div>
    <div class="ps-scrollbar-y-rail" style="top: 0px; right: 3px;">
        <div class="ps-scrollbar-y" tabindex="0" style="top: 0px; height: 0px;"></div>
    </div>
</div>
java selenium selenium-webdriver cucumber gherkin
2个回答
1
投票

你缺少PageFactory的初始化,这就是为什么你得到NullPointerException而不是NoSuchElementException

像这样添加构造函数:

public class tableTestStepDefs {

@FindBy(how = How.XPATH, using = "//*[@id=\"contenido-table\"]/tbody/tr[1]/td[3]")
public WebElement cell1;
@FindBy(how = How.ID, using = "\"DTE_Field_sufijo\"")
public WebElement editableCell1;

public tableTestStepDefs() { //this!
    PageFactory.initElements(driver, this);
}

@When("^i click on cell1 to make it editable$")
public void hagoClickEnUnaCeldaDeLaColumnaSufijo(){
driver.get("http://192.168.242.104:7777/some/context");
cell1.click();
}

1
投票

你的一个FindBy语句是错误的。

@FindBy(how = How.ID, using = "\"DTE_Field_sufijo\"")

您说ID包含额外的引号。它应该是:

@FindBy(how = How.ID, using = "DTE_Field_sufijo")

尝试进行更改,看看它是否有效。否则,您需要包含实际的错误消息以帮助确定问题所在。

© www.soinside.com 2019 - 2024. All rights reserved.