【问题标题】:How to build a method to iterate through WebElements and return the one that is displayed using Selenium & Java?如何构建一种方法来遍历 WebElements 并返回使用 Selenium 和 Java 显示的方法?
【发布时间】:2019-03-13 18:08:59
【问题描述】:

因此,页面充满了许多类似的属性,例如下面这两个元素。 例子:

<button id="exibe_codigo_de_barras_210888-1" data-context="minhas-contas_conta_codigo-barras_btn-ver-codigo-barras-colapse" font-size="0.875rem" width="100%" class="styled__Button-sc-6mz6kj-0 gMrMBo" style="" xpath="1">VER CÓDIGO DE BARRAS <span class="sc-1o2smz3-2 dAGdGd styled__Arrow-tuh5ii-0 iYDeiw"><svg width="12px" height="8px" viewBox="0 0 12 8" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><defs><polygon id="chevron" points="7.41 8 12 12.9446694 16.59 8 18 9.52226721 12 16 6 9.52226721"></polygon></defs><g id="Symbols" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd"><g id="Interface/Seta-expandir" transform="translate(-6.000000, -8.000000)"><mask id="mask-2" fill="white"><use xlink:href="#chevron"></use></mask><use id="Seta-expandir" fill="#D82482" fill-rule="nonzero" xlink:href="#chevron"></use></g></g></svg></span></button>

<button id="exibe_codigo_de_barras_210888-2" data-context="minhas-contas_conta_codigo-barras_btn-ver-codigo-barras-colapse" font-size="0.875rem" width="100%" class="styled__Button-sc-6mz6kj-0 gMrMBo" style="" xpath="1">VER CÓDIGO DE BARRAS <span class="sc-1o2smz3-2 dAGdGd styled__Arrow-tuh5ii-0 iYDeiw"><svg width="12px" height="8px" viewBox="0 0 12 8" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><defs><polygon id="chevron" points="7.41 8 12 12.9446694 16.59 8 18 9.52226721 12 16 6 9.52226721"></polygon></defs><g id="Symbols" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd"><g id="Interface/Seta-expandir" transform="translate(-6.000000, -8.000000)"><mask id="mask-2" fill="white"><use xlink:href="#chevron"></use></mask><use id="Seta-expandir" fill="#D82482" fill-rule="nonzero" xlink:href="#chevron"></use></g></g></svg></span></button>

我正在使用 PageObject 和 PageFactory 来获取元素并将它们放入这样的数组中:

@FindAll(@FindBy(how = How.CSS, using = "[data-context=minhas-contas_conta_codigo-barras_btn-ver-codigo-barras-colapse]"))
public List<WebElement> btnVerCodigoDeBarras;

问题是这样的:我需要在不使用索引的情况下单击这些类似按钮之一(因为事实证明维护起来很麻烦)。我想到了一种方法,可以收集列表的所有 webElements 并返回显示在屏幕上的单个元素(我对新想法持开放态度),所以这是我尝试过的两种方法,但均未成功:

public static WebElement WaitForElementVisibleInArray(List<WebElement> elements) {
    boolean arrayNotEmpty = elements.size() > 0;
    if (arrayNotEmpty) {
        for (Iterator<WebElement> i = elements.iterator(); i.hasNext(); ) {
            WebElement element = i.next();
            if (element.isEnabled() && element.isDisplayed()) {
                return element;
            }
        }
    }
    return null;
}

public static WebElement WaitForElementVisibleInArray(List<WebElement> elements) {
    boolean arrayNotEmpty = elements.size() > 0;        
    if (arrayNotEmpty) {            
        Iterator<WebElement> it = elements.iterator();
        WebElement element;
        while (it.hasNext()) {
            element = it.next();
            if (element.isEnabled() && element.isDisplayed()) {
                return element;
            }
        }
    }
    return null;
}

我搜索了很多,无法解决这个问题。提前谢谢!

编辑:

根据要求,下面是包含多个 div 子项的 div 父项的两个示例: 第一个 div:

&lt;div class="sc-1o2smz3-8 bdzhlP styled__Row-sc-13w6d15-0 SpKwt"&gt;&lt;div&gt;&lt;button id="envia_codigo_de_barras_210888-1" data-context="minhas-contas_conta_codigo-barras_btn-enviar" class="sc-1o2smz3-5 gzODBO styled__Button-sc-6mz6kj-0 EOEkK" font-size="0.875rem" width="100%"&gt;&lt;svg fill="#D82482" width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"&gt;&lt;path fill="none" d="M0 0h24v24H0z"&gt;&lt;/path&gt;&lt;path d="M20 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 14H4V8l8 5 8-5v10zm-8-7L4 6h16l-8 5z"&gt;&lt;/path&gt;&lt;/svg&gt; Enviar&lt;/button&gt;&lt;div class="modal styled__Modal-a94357-0 fQRtzL"&gt;&lt;aside class="overlay styled__Overlay-gtv01n-0 joIHPo"&gt;&lt;/aside&gt;&lt;div radius="4" display="flex" direction="column" width="100%" height="auto" cursor="initial" order="0" class="Modal__Container-sc-13j9751-1 cbeGrw Container__ContainerStyle-sc-1iqy2ia-0 dVRtOm" style="max-width: 500px; display: flex; flex: 1 1 0%;"&gt;&lt;button class="fechar styled__Button-sc-6mz6kj-0 EOEkK" font-size="0.875rem" width="100%"&gt;&lt;svg width="30" height="30" viewBox="0 0 30 30"&gt;&lt;path fill="#9B9B9B" fill-rule="evenodd" d="M13.106 15.09L8.227 19.85l1.625 2.035 5.119-4.976 5.099 4.976 1.882-1.83-5.109-4.966 5.109-4.967-1.882-1.83-5.1 4.976-5.118-4.976-1.625 2.035 4.879 4.762zM15 30C6.716 30 0 23.284 0 15 0 6.716 6.716 0 15 0c8.284 0 15 6.716 15 15 0 8.284-6.716 15-15 15z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/button&gt;&lt;div class="Row styled__Row-sc-13w6d15-0 gPFeBI" data-context="popup_enviar-codigo-barras" style="width: 100%;"&gt;&lt;div class="col styled__Col-sc-4ph28m-0 bWdzYK"&gt;&lt;div class="Row styled__Row-sc-13w6d15-0 gPFeBI"&gt;&lt;h2 class="sc-1h9tg9u-1 ieQsBc Title__TitleStyle-sc-5b4olk-0 gZiyVB" font-weight="Regular" width="100%"&gt;Enviar código de barras&lt;/h2&gt;&lt;/div&gt;&lt;div class="Row styled__Row-sc-13w6d15-0 cXZsiu"&gt;&lt;div class="col styled__Col-sc-4ph28m-0 cEyrnf"&gt;&lt;label class="progress-bar styled__Radio-sc-13c6bcq-0 jIgdvS" data-context="popup_enviar-codigo-barras_input-radio_forma-envio" id="envio_email"&gt;&lt;input type="checkbox" checked=""&gt;&lt;span&gt;&lt;/span&gt;&lt;p font-size="18px" type="p" align="left" font-family="Simplon, OiTextRegular, simplon_bpregular, sans-serif" font-weight="Regular" fontcolor="#222222" themecolor="dark" margin="0" padding="0" width="100%" height="auto" lineheight="1em" letter-spacing="normal" whitespace="initial" maxwidth="initial" class="Text__TextStyle-fp0yjz-0 gTxJFk"&gt;&amp;nbsp;E-mail&lt;/p&gt;&lt;/label&gt;&lt;/div&gt;&lt;div class="Row styled__Row-sc-13w6d15-0 fKsxZO"&gt;&lt;/div&gt;&lt;div class="col styled__Col-sc-4ph28m-0 cEyrnf"&gt;&lt;label class="progress-bar styled__Radio-sc-13c6bcq-0 jIgdvS" data-context="popup_enviar-codigo-barras_input-radio_forma-envio" id="envio_sms"&gt;&lt;input type="checkbox"&gt;&lt;span&gt;&lt;/span&gt;&lt;p font-size="18px" type="p" align="left" font-family="Simplon, OiTextRegular, simplon_bpregular, sans-serif" font-weight="Regular" fontcolor="#222222" themecolor="dark" margin="0" padding="0" width="100%" height="auto" lineheight="1em" letter-spacing="normal" whitespace="initial" maxwidth="initial" class="Text__TextStyle-fp0yjz-0 gTxJFk"&gt;&amp;nbsp;SMS pra Celular Oi&lt;/p&gt;&lt;/label&gt;&lt;/div&gt;&lt;div class="col styled__Col-sc-4ph28m-0 cEyrnf"&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="Row styled__Row-sc-13w6d15-0 gPFeBI"&gt;&lt;div class="col styled__Col-sc-4ph28m-0 cKjXbt"&gt;&lt;fieldset class="styledFieldset-sc-1r2we57-0 jBoAvV"&gt;&lt;input class="sc-1h9tg9u-0 dFDlQp styledInput__Input-sc-1apvofq-0 bkjwCX" id="informe_email" data-context="popup_enviar-codigo-barras_input-text_email" placeholder="Digite seu e-mail" maxlength="50" value=""&gt;&lt;div class="sc-1h9tg9u-0 dFDlQp styledMessage-d8kpbw-0 gAaQEw"&gt;&lt;/div&gt;&lt;/fieldset&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="Row styled__Row-sc-13w6d15-0 kRKEGz"&gt;&lt;div class="col styled__Col-sc-4ph28m-0 cKjXbt"&gt;&lt;button disabled="" id="submit_envio" data-context="popup_enviar-codigo-barras_input-submit" font-size="0.875rem" width="100%" class="styled__Button-sc-6mz6kj-0 crrbD"&gt;ENVIAR&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;button data-context="minhas-contas_conta_codigo-barras_btn-copiar" id="copia_codigo_de_barras_210888-1" class="sc-1o2smz3-5 gzODBO styled__Button-sc-6mz6kj-0 EOEkK" font-size="0.875rem" width="100%"&gt;&lt;svg fill="#D82482" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"&gt;&lt;path fill="none" d="M0 0h24v24H0z"&gt;&lt;/path&gt;&lt;path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm-1 4l6 6v10c0 1.1-.9 2-2 2H7.99C6.89 23 6 22.1 6 21l.01-14c0-1.1.89-2 1.99-2h7zm-1 7h5.5L14 6.5V12z"&gt;&lt;/path&gt;&lt;/svg&gt; Copiar código&lt;/button&gt;&lt;button data-context="minhas-contas_conta_codigo-barras_btn-baixar-pdf" class="sc-1o2smz3-5 gzODBO styled__Button-sc-6mz6kj-0 EOEkK" font-size="0.875rem" width="100%"&gt;&lt;svg fill="#D82482" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"&gt;&lt;path d="M0 0h24v24H0z" fill="none"&gt;&lt;/path&gt;&lt;path d="M14 2H6c-1.1 0-1.99.9-1.99 2L4 20c0 1.1.89 2 1.99 2H18c1.1 0 2-.9 2-2V8l-6-6zm2 16H8v-2h8v2zm0-4H8v-2h8v2zm-3-5V3.5L18.5 9H13z"&gt;&lt;/path&gt;&lt;/svg&gt; Baixar PDF&lt;/button&gt;&lt;button id="imprime_fatura_210888-1" data-context="minhas-contas_conta_codigo-barras_btn-imprimir" class="sc-1o2smz3-5 gzODBO styled__Button-sc-6mz6kj-0 EOEkK" font-size="0.875rem" width="100%"&gt;&lt;svg fill="#D82482" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"&gt;&lt;path d="M19 8H5c-1.66 0-3 1.34-3 3v6h4v4h12v-4h4v-6c0-1.66-1.34-3-3-3zm-3 11H8v-5h8v5zm3-7c-.55 0-1-.45-1-1s.45-1 1-1 1 .45 1 1-.45 1-1 1zm-1-9H6v4h12V3z"&gt;&lt;/path&gt;&lt;path d="M0 0h24v24H0z" fill="none"&gt;&lt;/path&gt;&lt;/svg&gt; Imprimir&lt;/button&gt;&lt;/div&gt;

还有第二个div:

&lt;div class="sc-1o2smz3-0 dzkPUw styled__Row-sc-13w6d15-0 gPFeBI" data-context="minhas-contas_conta"&gt;&lt;div class="col styled__Col-sc-4ph28m-0 cTsLBT" width="100%"&gt;&lt;div class="Row styled__Row-sc-13w6d15-0 bTDNmX"&gt;&lt;div class="col styled__Col-sc-4ph28m-0 cEyrnf"&gt;&lt;div class="no-wrap styled__Row-sc-13w6d15-0 hfVmIP"&gt;&lt;div class="col styled__Col-sc-4ph28m-0 hAdyfM"&gt;&lt;label class="progress-bar styled__Radio-sc-13c6bcq-0 kXqgpo" id="seleciona_fatura_210888-2"&gt;&lt;input type="checkbox"&gt;&lt;span&gt;&lt;/span&gt;&lt;/label&gt;&lt;/div&gt;&lt;div class="col styled__Col-sc-4ph28m-0 lnnClR"&gt;&lt;div class="sc-1o2smz3-8 bdzhlP styled__Row-sc-13w6d15-0 fpsrZI"&gt;&lt;h3 font-family="SimplonMedium" data-context="minhas-contas_conta_valor" font-weight="Regular" width="100%" class="Title__TitleStyle-sc-5b4olk-0 kCdfIG"&gt;R$ &amp;nbsp;99,88&amp;nbsp;&lt;/h3&gt;&lt;/div&gt;&lt;p font-size="14px" lineheight="1.5em" type="p" align="left" font-family="Simplon, OiTextRegular, simplon_bpregular, sans-serif" font-weight="Regular" fontcolor="#222222" themecolor="dark" margin="0" padding="0" width="100%" height="auto" letter-spacing="normal" whitespace="initial" maxwidth="initial" class="Text__TextStyle-fp0yjz-0 bcIQEp"&gt;Vencimento: 11/03/2018&lt;/p&gt;&lt;div class="Row styled__Row-sc-13w6d15-0 gPFeBI" data-context="minhas-contas_conta_status"&gt;&lt;div font-size="14" width="auto" class="Alert__StyledAlert-huwm74-0 cOBlGy"&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;Vencida há &lt;strong data-context="minhas-contas_conta_tempo-vencimento"&gt;368 dias&lt;/strong&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="Row styled__Row-sc-13w6d15-0 fKsxZO"&gt;&lt;/div&gt;&lt;div class="col styled__Col-sc-4ph28m-0 kGpLSX"&gt;&lt;div class="Row styled__Row-sc-13w6d15-0 gPFeBI"&gt;&lt;div class="col styled__Col-sc-4ph28m-0 lnnClR"&gt;&lt;button id="baixa_pdf_210888-2" data-context="minhas-contas_produto_conta_btn-ver-boleto" font-size="0.875rem" width="100%" class="styled__Button-sc-6mz6kj-0 EOEkK"&gt;Baixar conta em PDF&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="Row styled__Row-sc-13w6d15-0 gPFeBI"&gt;&lt;/div&gt;&lt;div class="Row styled__Row-sc-13w6d15-0 fKsxZO"&gt;&lt;/div&gt;&lt;div class="col styled__Col-sc-4ph28m-0 jtUyYJ"&gt;&lt;button id="exibe_codigo_de_barras_210888-2" data-context="minhas-contas_conta_codigo-barras_btn-ver-codigo-barras" font-size="0.875rem" width="100%" class="styled__Button-sc-6mz6kj-0 gMrMBo"&gt;VER CÓDIGO DE BARRAS &lt;span class="sc-1o2smz3-2 dAGdGd styled__Arrow-tuh5ii-0 dYwLxl"&gt;&lt;svg width="12px" height="8px" viewBox="0 0 12 8" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"&gt;&lt;defs&gt;&lt;polygon id="chevron" points="7.41 8 12 12.9446694 16.59 8 18 9.52226721 12 16 6 9.52226721"&gt;&lt;/polygon&gt;&lt;/defs&gt;&lt;g id="Symbols" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd"&gt;&lt;g id="Interface/Seta-expandir" transform="translate(-6.000000, -8.000000)"&gt;&lt;mask id="mask-2" fill="white"&gt;&lt;use xlink:href="#chevron"&gt;&lt;/use&gt;&lt;/mask&gt;&lt;use id="Seta-expandir" fill="#D82482" fill-rule="nonzero" xlink:href="#chevron"&gt;&lt;/use&gt;&lt;/g&gt;&lt;/g&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;div height="260" class="sc-1o2smz3-6 dJMXeH Collapsible__CollapsibleContainer-sc-12167wd-0 gksWIz" width="100%" open=""&gt;&lt;div class="sc-1o2smz3-7 EuVAO"&gt;&lt;div class="col styled__Col-sc-4ph28m-0 feSHva"&gt;&lt;div class="Row styled__Row-sc-13w6d15-0 bVFhjc"&gt;&lt;svg width="648px" height="80px" x="0px" y="0px" viewBox="0 0 648 80" xmlns="http://www.w3.org/2000/svg" version="1.1" style="transform: translate(0,0)"&gt;&lt;rect x="0" y="0" width="648" height="80" style="fill:#ffffff;"&gt;&lt;/rect&gt;&lt;g transform="translate(0, 0)" style="fill:#000000;"&gt;&lt;rect x="0" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="3.2000000000000006" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="6.4" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="12.8" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="16" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="22.400000000000002" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="28.8" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="35.2" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="38.400000000000006" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="44.8" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="54.4" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="60.800000000000004" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="64.00000000000001" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="67.2" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="70.4" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="80.00000000000001" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="89.60000000000001" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="92.80000000000001" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="96.00000000000001" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="99.2" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="108.80000000000001" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="118.4" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="121.60000000000001" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="124.80000000000001" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="128" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="137.6" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="147.20000000000002" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="150.4" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="153.60000000000002" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="160" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="166.4" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="176.00000000000003" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="179.20000000000002" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="185.6" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="192.00000000000003" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="195.2" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="204.8" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="208" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="214.4" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="217.60000000000002" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="224" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="233.60000000000002" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="236.8" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="246.4" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="249.60000000000002" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="252.8" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="256" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="265.6" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="272" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="281.59999999999997" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="284.8" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="288" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="294.4" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="297.6" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="304" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="310.4" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="313.6" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="323.2" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="332.8" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="336" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="342.4" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="345.6" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="352" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="361.6" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="364.8" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="374.4" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="377.6" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="380.8" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="387.2" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="393.6" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="396.8" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="406.4" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="409.6" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="416" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="419.2" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="425.6" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="435.2" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="438.4" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="441.6" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="448" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="454.40000000000003" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="464" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="467.2" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="470.4" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="473.6" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="483.2" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="492.8" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="496" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="502.4" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="505.6" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="515.2" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="521.6" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="524.8000000000001" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="531.2" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="537.6" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="544" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="547.2" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="553.6" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="560" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="563.2" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="569.6" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="576.0000000000001" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="582.4" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="585.6" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="588.8000000000001" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="598.4000000000001" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="608" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="611.2" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="614.4" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="617.6000000000001" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="627.2" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="636.8000000000001" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;rect x="640.0000000000001" y="0" width="4.800000000000001" height="80"&gt;&lt;/rect&gt;&lt;rect x="646.4000000000001" y="0" width="1.6" height="80"&gt;&lt;/rect&gt;&lt;/g&gt;&lt;/svg&gt;&lt;/div&gt;&lt;p align="center" font-weight="Bold" lineheight="1.5" type="p" font-family="Simplon, OiTextRegular, simplon_bpregular, sans-serif" font-size="16" fontcolor="#222222" themecolor="dark" margin="0" padding="0" width="100%" height="auto" letter-spacing="normal" whitespace="initial" maxwidth="initial" class="Text__TextStyle-fp0yjz-0 iWZvwv"&gt;84600000000-6  99880113224-7  51558980900-0  00516100000-4&lt;/p&gt;&lt;/div&gt;&lt;div class="Row styled__Row-sc-13w6d15-0 kHQmEI"&gt;&lt;div class="sc-1o2smz3-9 jjdcZL Alert__StyledAlert-huwm74-0 cOBlGy" font-size="14" width="auto"&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;p class="sc-1o2smz3-3 kPGwia Text__TextStyle-fp0yjz-0 beCits" type="p" align="left" font-family="Simplon, OiTextRegular, simplon_bpregular, sans-serif" font-weight="Regular" font-size="16" fontcolor="#222222" themecolor="dark" margin="0" padding="0" width="100%" height="auto" lineheight="1em" letter-spacing="normal" whitespace="initial" maxwidth="initial"&gt;Lembre-se que esta conta está cadastrada em Débito Automático.&lt;br&gt;Se realizar o pagamento agora, você deverá entrar em contato com seu banco pra retirar o débito programado.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="sc-1o2smz3-8 bdzhlP styled__Row-sc-13w6d15-0 SpKwt"&gt;&lt;div&gt;&lt;button id="envia_codigo_de_barras_210888-2" data-context="minhas-contas_conta_codigo-barras_btn-enviar" class="sc-1o2smz3-5 gzODBO styled__Button-sc-6mz6kj-0 EOEkK" font-size="0.875rem" width="100%"&gt;&lt;svg fill="#D82482" width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"&gt;&lt;path fill="none" d="M0 0h24v24H0z"&gt;&lt;/path&gt;&lt;path d="M20 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 14H4V8l8 5 8-5v10zm-8-7L4 6h16l-8 5z"&gt;&lt;/path&gt;&lt;/svg&gt; Enviar&lt;/button&gt;&lt;div class="modal styled__Modal-a94357-0 fQRtzL"&gt;&lt;aside class="overlay styled__Overlay-gtv01n-0 joIHPo"&gt;&lt;/aside&gt;&lt;div radius="4" display="flex" direction="column" width="100%" height="auto" cursor="initial" order="0" class="Modal__Container-sc-13j9751-1 cbeGrw Container__ContainerStyle-sc-1iqy2ia-0 dVRtOm" style="max-width: 500px; display: flex; flex: 1 1 0%;"&gt;&lt;button class="fechar styled__Button-sc-6mz6kj-0 EOEkK" font-size="0.875rem" width="100%"&gt;&lt;svg width="30" height="30" viewBox="0 0 30 30"&gt;&lt;path fill="#9B9B9B" fill-rule="evenodd" d="M13.106 15.09L8.227 19.85l1.625 2.035 5.119-4.976 5.099 4.976 1.882-1.83-5.109-4.966 5.109-4.967-1.882-1.83-5.1 4.976-5.118-4.976-1.625 2.035 4.879 4.762zM15 30C6.716 30 0 23.284 0 15 0 6.716 6.716 0 15 0c8.284 0 15 6.716 15 15 0 8.284-6.716 15-15 15z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/button&gt;&lt;div class="Row styled__Row-sc-13w6d15-0 gPFeBI" data-context="popup_enviar-codigo-barras" style="width: 100%;"&gt;&lt;div class="col styled__Col-sc-4ph28m-0 bWdzYK"&gt;&lt;div class="Row styled__Row-sc-13w6d15-0 gPFeBI"&gt;&lt;h2 class="sc-1h9tg9u-1 ieQsBc Title__TitleStyle-sc-5b4olk-0 gZiyVB" font-weight="Regular" width="100%"&gt;Enviar código de barras&lt;/h2&gt;&lt;/div&gt;&lt;div class="Row styled__Row-sc-13w6d15-0 cXZsiu"&gt;&lt;div class="col styled__Col-sc-4ph28m-0 cEyrnf"&gt;&lt;label class="progress-bar styled__Radio-sc-13c6bcq-0 jIgdvS" data-context="popup_enviar-codigo-barras_input-radio_forma-envio" id="envio_email"&gt;&lt;input type="checkbox" checked=""&gt;&lt;span&gt;&lt;/span&gt;&lt;p font-size="18px" type="p" align="left" font-family="Simplon, OiTextRegular, simplon_bpregular, sans-serif" font-weight="Regular" fontcolor="#222222" themecolor="dark" margin="0" padding="0" width="100%" height="auto" lineheight="1em" letter-spacing="normal" whitespace="initial" maxwidth="initial" class="Text__TextStyle-fp0yjz-0 gTxJFk"&gt;&amp;nbsp;E-mail&lt;/p&gt;&lt;/label&gt;&lt;/div&gt;&lt;div class="Row styled__Row-sc-13w6d15-0 fKsxZO"&gt;&lt;/div&gt;&lt;div class="col styled__Col-sc-4ph28m-0 cEyrnf"&gt;&lt;label class="progress-bar styled__Radio-sc-13c6bcq-0 jIgdvS" data-context="popup_enviar-codigo-barras_input-radio_forma-envio" id="envio_sms"&gt;&lt;input type="checkbox"&gt;&lt;span&gt;&lt;/span&gt;&lt;p font-size="18px" type="p" align="left" font-family="Simplon, OiTextRegular, simplon_bpregular, sans-serif" font-weight="Regular" fontcolor="#222222" themecolor="dark" margin="0" padding="0" width="100%" height="auto" lineheight="1em" letter-spacing="normal" whitespace="initial" maxwidth="initial" class="Text__TextStyle-fp0yjz-0 gTxJFk"&gt;&amp;nbsp;SMS pra Celular Oi&lt;/p&gt;&lt;/label&gt;&lt;/div&gt;&lt;div class="col styled__Col-sc-4ph28m-0 cEyrnf"&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="Row styled__Row-sc-13w6d15-0 gPFeBI"&gt;&lt;div class="col styled__Col-sc-4ph28m-0 cKjXbt"&gt;&lt;fieldset class="styledFieldset-sc-1r2we57-0 jBoAvV"&gt;&lt;input class="sc-1h9tg9u-0 dFDlQp styledInput__Input-sc-1apvofq-0 bkjwCX" id="informe_email" data-context="popup_enviar-codigo-barras_input-text_email" placeholder="Digite seu e-mail" maxlength="50" value=""&gt;&lt;div class="sc-1h9tg9u-0 dFDlQp styledMessage-d8kpbw-0 gAaQEw"&gt;&lt;/div&gt;&lt;/fieldset&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="Row styled__Row-sc-13w6d15-0 kRKEGz"&gt;&lt;div class="col styled__Col-sc-4ph28m-0 cKjXbt"&gt;&lt;button disabled="" id="submit_envio" data-context="popup_enviar-codigo-barras_input-submit" font-size="0.875rem" width="100%" class="styled__Button-sc-6mz6kj-0 crrbD"&gt;ENVIAR&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;button data-context="minhas-contas_conta_codigo-barras_btn-copiar" id="copia_codigo_de_barras_210888-2" class="sc-1o2smz3-5 gzODBO styled__Button-sc-6mz6kj-0 EOEkK" font-size="0.875rem" width="100%"&gt;&lt;svg fill="#D82482" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"&gt;&lt;path fill="none" d="M0 0h24v24H0z"&gt;&lt;/path&gt;&lt;path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm-1 4l6 6v10c0 1.1-.9 2-2 2H7.99C6.89 23 6 22.1 6 21l.01-14c0-1.1.89-2 1.99-2h7zm-1 7h5.5L14 6.5V12z"&gt;&lt;/path&gt;&lt;/svg&gt; Copiar código&lt;/button&gt;&lt;button data-context="minhas-contas_conta_codigo-barras_btn-baixar-pdf" class="sc-1o2smz3-5 gzODBO styled__Button-sc-6mz6kj-0 EOEkK" font-size="0.875rem" width="100%"&gt;&lt;svg fill="#D82482" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"&gt;&lt;path d="M0 0h24v24H0z" fill="none"&gt;&lt;/path&gt;&lt;path d="M14 2H6c-1.1 0-1.99.9-1.99 2L4 20c0 1.1.89 2 1.99 2H18c1.1 0 2-.9 2-2V8l-6-6zm2 16H8v-2h8v2zm0-4H8v-2h8v2zm-3-5V3.5L18.5 9H13z"&gt;&lt;/path&gt;&lt;/svg&gt; Baixar PDF&lt;/button&gt;&lt;button id="imprime_fatura_210888-2" data-context="minhas-contas_conta_codigo-barras_btn-imprimir" class="sc-1o2smz3-5 gzODBO styled__Button-sc-6mz6kj-0 EOEkK" font-size="0.875rem" width="100%"&gt;&lt;svg fill="#D82482" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"&gt;&lt;path d="M19 8H5c-1.66 0-3 1.34-3 3v6h4v4h12v-4h4v-6c0-1.66-1.34-3-3-3zm-3 11H8v-5h8v5zm3-7c-.55 0-1-.45-1-1s.45-1 1-1 1 .45 1 1-.45 1-1 1zm-1-9H6v4h12V3z"&gt;&lt;/path&gt;&lt;path d="M0 0h24v24H0z" fill="none"&gt;&lt;/path&gt;&lt;/svg&gt; Imprimir&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;div class="modal styled__Modal-a94357-0 fQRtzL"&gt;&lt;aside class="overlay styled__Overlay-gtv01n-0 joIHPo"&gt;&lt;/aside&gt;&lt;div radius="4" display="flex" direction="column" width="100%" height="auto" cursor="initial" order="0" class="Modal__Container-sc-13j9751-1 cbeGrw Container__ContainerStyle-sc-1iqy2ia-0 dVRtOm"&gt;&lt;button class="fechar styled__Button-sc-6mz6kj-0 EOEkK" font-size="0.875rem" width="100%"&gt;&lt;svg width="30" height="30" viewBox="0 0 30 30"&gt;&lt;path fill="#9B9B9B" fill-rule="evenodd" d="M13.106 15.09L8.227 19.85l1.625 2.035 5.119-4.976 5.099 4.976 1.882-1.83-5.109-4.966 5.109-4.967-1.882-1.83-5.1 4.976-5.118-4.976-1.625 2.035 4.879 4.762zM15 30C6.716 30 0 23.284 0 15 0 6.716 6.716 0 15 0c8.284 0 15 6.716 15 15 0 8.284-6.716 15-15 15z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/button&gt;&lt;div class="sc-1o2smz3-1 gwBrau styled__Row-sc-13w6d15-0 gCsrNl"&gt;&lt;div font-size="14" width="auto" class="Alert__StyledAlert-huwm74-0 cOBlGy"&gt;&lt;div&gt;&lt;i size="20" width="auto" height="auto" transform="initial" class="Icon__StyledIcon-t66fqd-0 ilQPJq"&gt;&lt;svg viewBox="0 0 30 30" version="1.1" xmlns="http://www.w3.org/2000/svg"&gt;&lt;g fill="#f8562c"&gt;&lt;path d="M15,0 C6.72,0 0,6.72 0,15 C0,23.28 6.72,30 15,30 C23.28,30 30,23.28 30,15 C30,6.72 23.28,0 15,0 Z M16.5,22.5 L13.5,22.5 L13.5,19.5 L16.5,19.5 L16.5,22.5 Z M16.5,16.5 L13.5,16.5 L13.5,7.5 L16.5,7.5 L16.5,16.5 Z"&gt;&lt;/path&gt;&lt;/g&gt;&lt;/svg&gt;&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;p color="#222" type="p" align="left" font-family="Simplon, OiTextRegular, simplon_bpregular, sans-serif" font-weight="Regular" font-size="16" fontcolor="#222222" themecolor="dark" margin="0" padding="0" width="100%" height="auto" lineheight="1em" letter-spacing="normal" whitespace="initial" maxwidth="initial" class="Text__TextStyle-fp0yjz-0 beCits"&gt;Ocorreu um erro ao baixar o arquivo.&lt;br&gt;Tente novamente mais tarde.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

【问题讨论】:

  • 你得到什么错误?
  • 元素 = 空。因此,NullPointerException
  • 发布抛出 NPE 的代码。鉴于您的代码,这不应该发生。启用并显示的元素不能为空。你确定定位器是好的?也许没有任何元素可见/启用?
  • @FelipeLuz 用更多的 outerHTML 更新问题,以及您要定位/单击的确切元素
  • 我更新了问题:)

标签: java selenium pageobjects page-factory


【解决方案1】:

我将展示我将如何处理它,但问题可能是 selenium 并不总是根据元素类型正确返回 isDisplayed() 的问题。

public WebElement getFirstVisibleElement(List<WebELement> elements) {
    for (WebElement ele : elements)
        if (ele.isEnabled() && ele.isDisplayed())
            return ele;
    return null;
}

检查 Web 元素是否显示的更可靠方法是检查其尺寸(高度和宽度)。如果它们都为零,则不显示。

public WebElement getFirstVisibleElement(List<WebELement> elements) {
    for (WebElement ele : elements)
        if (ele.isEnabled() && ele.getSize().getHeight() > 0 && ele.getSize().getWidth() > 0 )
            return ele;
    return null;
}

【讨论】:

  • 您好,谢谢您的回答!在第一种方法中,元素为空,因此它显示空点异常。第二种方法是收集页面底部比较多的元素,不知道为什么
  • 实际上,在第一种方法中,循环不会返回'ele'
  • 您必须预料到 null 返回并从对该方法的调用中检查它,否则,您需要以其他方式处理它,但是会出现没有可见元素的情况在数组中,因此您需要在测试中相应地计划和采取行动。我会在代码中放置一个断点,以在测试启用和显示的行上停止,并一次检查每个元素的值 - 与第二种方法相同。也许您可以详细说明您正在使用的内容。
  • 我现在正在这样做,您的代码运行良好。谢谢!!
猜你喜欢
  • 1970-01-01
  • 2018-11-27
  • 2014-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-11
  • 1970-01-01
  • 2014-06-23
相关资源
最近更新 更多