GVKun编程网logo

如何构建一种方法来遍历WebElements并返回使用Selenium和Java显示的方法?

27

针对如何构建一种方法来遍历WebElements并返回使用Selenium和Java显示的方法?这个问题,本篇文章进行了详细的解答,同时本文还将给你拓展HandleAJAXelementsinSele

针对如何构建一种方法来遍历WebElements并返回使用Selenium和Java显示的方法?这个问题,本篇文章进行了详细的解答,同时本文还将给你拓展Handle AJAX elements in Selenium 2 (WebDriver)、Java Selenium封装--RemoteWebElement、Java Selenium,如何从链接WebElement获取linkText(锚)、java – WebElement或WebDriver调用findElement方法?等相关知识,希望可以帮助到你。

本文目录一览:

如何构建一种方法来遍历WebElements并返回使用Selenium和Java显示的方法?

如何构建一种方法来遍历WebElements并返回使用Selenium和Java显示的方法?

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

<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%"xpath="1">VER CÓDIGO DE BARRAS <span><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%"xpath="1">VER CÓDIGO DE BARRAS <span><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;

问题是这样的:我需要单击这些类似的按钮之一,而不使用索引( 因为事实证明,要维护它很麻烦
)。我想到了一种方法,我可以收集列表中的所有webElement并返回显示在屏幕上的单个webElement(我乐于接受新想法),所以这是我尝试的两种方法,但均未成功:

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:

<div><div><button id="envia_codigo_de_barras_210888-1" data-context="minhas-contas_conta_codigo-barras_btn-enviar"font-size="0.875rem" width="100%"><svg fill="#D82482" width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"></path><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"></path></svg> Enviar</button><div><aside></aside><div radius="4" display="flex" direction="column" width="100%" height="auto" cursor="initial" order="0"><buttonfont-size="0.875rem" width="100%"><svg width="30" height="30" viewBox="0 0 30 30"><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"></path></svg></button><divdata-context="popup_enviar-codigo-barras"><div><div><h2font-weight="Regular" width="100%">Enviar código de barras</h2></div><div><div><labeldata-context="popup_enviar-codigo-barras_input-radio_forma-envio" id="envio_email"><input type="checkbox" checked=""><span></span><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">&nbsp;E-mail</p></label></div><div></div><div><labeldata-context="popup_enviar-codigo-barras_input-radio_forma-envio" id="envio_sms"><input type="checkbox"><span></span><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">&nbsp;SMS pra Celular Oi</p></label></div><div></div></div><div><div><fieldset><inputid="informe_email" data-context="popup_enviar-codigo-barras_input-text_email" placeholder="Digite seu e-mail" maxlength="50" value=""><div></div></fieldset></div></div><div><div><button disabled="" id="submit_envio" data-context="popup_enviar-codigo-barras_input-submit" font-size="0.875rem" width="100%">ENVIAR</button></div></div></div></div></div></div></div><button data-context="minhas-contas_conta_codigo-barras_btn-copiar" id="copia_codigo_de_barras_210888-1"font-size="0.875rem" width="100%"><svg fill="#D82482" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"></path><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"></path></svg> Copiar código</button><button data-context="minhas-contas_conta_codigo-barras_btn-baixar-pdf"font-size="0.875rem" width="100%"><svg fill="#D82482" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="M0 0h24v24H0z" fill="none"></path><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"></path></svg> Baixar PDF</button><button id="imprime_fatura_210888-1" data-context="minhas-contas_conta_codigo-barras_btn-imprimir"font-size="0.875rem" width="100%"><svg fill="#D82482" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><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"></path><path d="M0 0h24v24H0z" fill="none"></path></svg> Imprimir</button></div>

第二个div:

<divdata-context="minhas-contas_conta"><divwidth="100%"><div><div><div><div><labelid="seleciona_fatura_210888-2"><input type="checkbox"><span></span></label></div><div><div><h3 font-family="SimplonMedium" data-context="minhas-contas_conta_valor" font-weight="Regular" width="100%">R$ &nbsp;99,88&nbsp;</h3></div><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">Vencimento: 11/03/2018</p><divdata-context="minhas-contas_conta_status"><div font-size="14" width="auto"><div></div><div>Vencida há <strong data-context="minhas-contas_conta_tempo-vencimento">368 dias</strong></div></div></div></div></div></div><div></div><div><div><div><button id="baixa_pdf_210888-2" data-context="minhas-contas_produto_conta_btn-ver-boleto" font-size="0.875rem" width="100%">Baixar conta em PDF</button></div></div></div><div></div><div></div><div><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%">VER CÓDIGO DE BARRAS <span><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></div></div><div height="260"width="100%" open=""><div><div><div><svg width="648px" height="80px" x="0px" y="0px" viewBox="0 0 648 80" xmlns="http://www.w3.org/2000/svg" version="1.1"><rect x="0" y="0" width="648" height="80"></rect><g transform="translate(0, 0)"><rect x="0" y="0" width="1.6" height="80"></rect><rect x="3.2000000000000006" y="0" width="1.6" height="80"></rect><rect x="6.4" y="0" width="4.800000000000001" height="80"></rect><rect x="12.8" y="0" width="1.6" height="80"></rect><rect x="16" y="0" width="1.6" height="80"></rect><rect x="22.400000000000002" y="0" width="4.800000000000001" height="80"></rect><rect x="28.8" y="0" width="1.6" height="80"></rect><rect x="35.2" y="0" width="1.6" height="80"></rect><rect x="38.400000000000006" y="0" width="4.800000000000001" height="80"></rect><rect x="44.8" y="0" width="4.800000000000001" height="80"></rect><rect x="54.4" y="0" width="1.6" height="80"></rect><rect x="60.800000000000004" y="0" width="1.6" height="80"></rect><rect x="64.00000000000001" y="0" width="1.6" height="80"></rect><rect x="67.2" y="0" width="1.6" height="80"></rect><rect x="70.4" y="0" width="4.800000000000001" height="80"></rect><rect x="80.00000000000001" y="0" width="4.800000000000001" height="80"></rect><rect x="89.60000000000001" y="0" width="1.6" height="80"></rect><rect x="92.80000000000001" y="0" width="1.6" height="80"></rect><rect x="96.00000000000001" y="0" width="1.6" height="80"></rect><rect x="99.2" y="0" width="4.800000000000001" height="80"></rect><rect x="108.80000000000001" y="0" width="4.800000000000001" height="80"></rect><rect x="118.4" y="0" width="1.6" height="80"></rect><rect x="121.60000000000001" y="0" width="1.6" height="80"></rect><rect x="124.80000000000001" y="0" width="1.6" height="80"></rect><rect x="128" y="0" width="4.800000000000001" height="80"></rect><rect x="137.6" y="0" width="4.800000000000001" height="80"></rect><rect x="147.20000000000002" y="0" width="1.6" height="80"></rect><rect x="150.4" y="0" width="1.6" height="80"></rect><rect x="153.60000000000002" y="0" width="1.6" height="80"></rect><rect x="160" y="0" width="4.800000000000001" height="80"></rect><rect x="166.4" y="0" width="4.800000000000001" height="80"></rect><rect x="176.00000000000003" y="0" width="1.6" height="80"></rect><rect x="179.20000000000002" y="0" width="1.6" height="80"></rect><rect x="185.6" y="0" width="4.800000000000001" height="80"></rect><rect x="192.00000000000003" y="0" width="1.6" height="80"></rect><rect x="195.2" y="0" width="4.800000000000001" height="80"></rect><rect x="204.8" y="0" width="1.6" height="80"></rect><rect x="208" y="0" width="4.800000000000001" height="80"></rect><rect x="214.4" y="0" width="1.6" height="80"></rect><rect x="217.60000000000002" y="0" width="1.6" height="80"></rect><rect x="224" y="0" width="4.800000000000001" height="80"></rect><rect x="233.60000000000002" y="0" width="1.6" height="80"></rect><rect x="236.8" y="0" width="4.800000000000001" height="80"></rect><rect x="246.4" y="0" width="1.6" height="80"></rect><rect x="249.60000000000002" y="0" width="1.6" height="80"></rect><rect x="252.8" y="0" width="1.6" height="80"></rect><rect x="256" y="0" width="4.800000000000001" height="80"></rect><rect x="265.6" y="0" width="4.800000000000001" height="80"></rect><rect x="272" y="0" width="4.800000000000001" height="80"></rect><rect x="281.59999999999997" y="0" width="1.6" height="80"></rect><rect x="284.8" y="0" width="1.6" height="80"></rect><rect x="288" y="0" width="1.6" height="80"></rect><rect x="294.4" y="0" width="1.6" height="80"></rect><rect x="297.6" y="0" width="4.800000000000001" height="80"></rect><rect x="304" y="0" width="1.6" height="80"></rect><rect x="310.4" y="0" width="1.6" height="80"></rect><rect x="313.6" y="0" width="4.800000000000001" height="80"></rect><rect x="323.2" y="0" width="4.800000000000001" height="80"></rect><rect x="332.8" y="0" width="1.6" height="80"></rect><rect x="336" y="0" width="4.800000000000001" height="80"></rect><rect x="342.4" y="0" width="1.6" height="80"></rect><rect x="345.6" y="0" width="1.6" height="80"></rect><rect x="352" y="0" width="4.800000000000001" height="80"></rect><rect x="361.6" y="0" width="1.6" height="80"></rect><rect x="364.8" y="0" width="4.800000000000001" height="80"></rect><rect x="374.4" y="0" width="1.6" height="80"></rect><rect x="377.6" y="0" width="1.6" height="80"></rect><rect x="380.8" y="0" width="4.800000000000001" height="80"></rect><rect x="387.2" y="0" width="1.6" height="80"></rect><rect x="393.6" y="0" width="1.6" height="80"></rect><rect x="396.8" y="0" width="4.800000000000001" height="80"></rect><rect x="406.4" y="0" width="1.6" height="80"></rect><rect x="409.6" y="0" width="4.800000000000001" height="80"></rect><rect x="416" y="0" width="1.6" height="80"></rect><rect x="419.2" y="0" width="1.6" height="80"></rect><rect x="425.6" y="0" width="4.800000000000001" height="80"></rect><rect x="435.2" y="0" width="1.6" height="80"></rect><rect x="438.4" y="0" width="1.6" height="80"></rect><rect x="441.6" y="0" width="4.800000000000001" height="80"></rect><rect x="448" y="0" width="1.6" height="80"></rect><rect x="454.40000000000003" y="0" width="4.800000000000001" height="80"></rect><rect x="464" y="0" width="1.6" height="80"></rect><rect x="467.2" y="0" width="1.6" height="80"></rect><rect x="470.4" y="0" width="1.6" height="80"></rect><rect x="473.6" y="0" width="4.800000000000001" height="80"></rect><rect x="483.2" y="0" width="4.800000000000001" height="80"></rect><rect x="492.8" y="0" width="1.6" height="80"></rect><rect x="496" y="0" width="1.6" height="80"></rect><rect x="502.4" y="0" width="1.6" height="80"></rect><rect x="505.6" y="0" width="4.800000000000001" height="80"></rect><rect x="515.2" y="0" width="4.800000000000001" height="80"></rect><rect x="521.6" y="0" width="1.6" height="80"></rect><rect x="524.8000000000001" y="0" width="4.800000000000001" height="80"></rect><rect x="531.2" y="0" width="1.6" height="80"></rect><rect x="537.6" y="0" width="1.6" height="80"></rect><rect x="544" y="0" width="1.6" height="80"></rect><rect x="547.2" y="0" width="4.800000000000001" height="80"></rect><rect x="553.6" y="0" width="4.800000000000001" height="80"></rect><rect x="560" y="0" width="1.6" height="80"></rect><rect x="563.2" y="0" width="1.6" height="80"></rect><rect x="569.6" y="0" width="1.6" height="80"></rect><rect x="576.0000000000001" y="0" width="4.800000000000001" height="80"></rect><rect x="582.4" y="0" width="1.6" height="80"></rect><rect x="585.6" y="0" width="1.6" height="80"></rect><rect x="588.8000000000001" y="0" width="4.800000000000001" height="80"></rect><rect x="598.4000000000001" y="0" width="4.800000000000001" height="80"></rect><rect x="608" y="0" width="1.6" height="80"></rect><rect x="611.2" y="0" width="1.6" height="80"></rect><rect x="614.4" y="0" width="1.6" height="80"></rect><rect x="617.6000000000001" y="0" width="4.800000000000001" height="80"></rect><rect x="627.2" y="0" width="4.800000000000001" height="80"></rect><rect x="636.8000000000001" y="0" width="1.6" height="80"></rect><rect x="640.0000000000001" y="0" width="4.800000000000001" height="80"></rect><rect x="646.4000000000001" y="0" width="1.6" height="80"></rect></g></svg></div><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">84600000000-6  99880113224-7  51558980900-0  00516100000-4</p></div><div><divfont-size="14" width="auto"><div></div><div><ptype="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">Lembre-se que esta conta está cadastrada em Débito Automático.<br>Se realizar o pagamento agora, você deverá entrar em contato com seu banco pra retirar o débito programado.</p></div></div></div><div><div><button id="envia_codigo_de_barras_210888-2" data-context="minhas-contas_conta_codigo-barras_btn-enviar"font-size="0.875rem" width="100%"><svg fill="#D82482" width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"></path><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"></path></svg> Enviar</button><div><aside></aside><div radius="4" display="flex" direction="column" width="100%" height="auto" cursor="initial" order="0"><buttonfont-size="0.875rem" width="100%"><svg width="30" height="30" viewBox="0 0 30 30"><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"></path></svg></button><divdata-context="popup_enviar-codigo-barras"><div><div><h2font-weight="Regular" width="100%">Enviar código de barras</h2></div><div><div><labeldata-context="popup_enviar-codigo-barras_input-radio_forma-envio" id="envio_email"><input type="checkbox" checked=""><span></span><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">&nbsp;E-mail</p></label></div><div></div><div><labeldata-context="popup_enviar-codigo-barras_input-radio_forma-envio" id="envio_sms"><input type="checkbox"><span></span><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">&nbsp;SMS pra Celular Oi</p></label></div><div></div></div><div><div><fieldset><inputid="informe_email" data-context="popup_enviar-codigo-barras_input-text_email" placeholder="Digite seu e-mail" maxlength="50" value=""><div></div></fieldset></div></div><div><div><button disabled="" id="submit_envio" data-context="popup_enviar-codigo-barras_input-submit" font-size="0.875rem" width="100%">ENVIAR</button></div></div></div></div></div></div></div><button data-context="minhas-contas_conta_codigo-barras_btn-copiar" id="copia_codigo_de_barras_210888-2"font-size="0.875rem" width="100%"><svg fill="#D82482" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"></path><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"></path></svg> Copiar código</button><button data-context="minhas-contas_conta_codigo-barras_btn-baixar-pdf"font-size="0.875rem" width="100%"><svg fill="#D82482" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="M0 0h24v24H0z" fill="none"></path><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"></path></svg> Baixar PDF</button><button id="imprime_fatura_210888-2" data-context="minhas-contas_conta_codigo-barras_btn-imprimir"font-size="0.875rem" width="100%"><svg fill="#D82482" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><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"></path><path d="M0 0h24v24H0z" fill="none"></path></svg> Imprimir</button></div></div></div></div><div><div><aside></aside><div radius="4" display="flex" direction="column" width="100%" height="auto" cursor="initial" order="0"><buttonfont-size="0.875rem" width="100%"><svg width="30" height="30" viewBox="0 0 30 30"><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"></path></svg></button><div><div font-size="14" width="auto"><div><i size="20" width="auto" height="auto" transform="initial"><svg viewBox="0 0 30 30" version="1.1" xmlns="http://www.w3.org/2000/svg"><g fill="#f8562c"><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"></path></g></svg></i></div><div><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">Ocorreu um erro ao baixar o arquivo.<br>Tente novamente mais tarde.</p></div></div></div></div></div></div></div>

答案1

小编典典

我将展示如何解决这个问题,但是问题可能出在硒元素不总是根据元素类型正确返回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;}

Handle AJAX elements in Selenium 2 (WebDriver)

Handle AJAX elements in Selenium 2 (WebDriver)

总结

以上是小编为你收集整理的Handle AJAX elements in Selenium 2 (WebDriver)全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

Java Selenium封装--RemoteWebElement

Java Selenium封装--RemoteWebElement

package com.selenium.driver;
import java.sql.SQLException;
import java.util.List;
import org.json.JSONException;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.internal.Coordinates;
import org.openqa.selenium.remote.RemoteWebElement;
import org.openqa.selenium.support.ui.Select;

public class JSWebElement {
	private RemoteWebElement we = null;
	private JavascriptExecutor jse = null;
	
	public JSWebElement(){}
	
	public JSWebElement(RemoteWebElement we){
		this.we = we;
	}
	
	///
	///通过元素ID定位元素
	///
	public JSWebElement findElementById(String using) {
		try {
			return new JSWebElement((RemoteWebElement)we.findElementById(using));
		}catch (NoSuchElementException e){
			return new JSWebElement();
		}
	}
	
	///
	///通过元素CSS表达式定位元素
	///
	public JSWebElement findElementByCssSelector(String using) {
		try {
			return new JSWebElement((RemoteWebElement)we.findElementByCssSelector(using));
		}catch (NoSuchElementException e){
			return new JSWebElement();
		}
	}

	///
	///通过元素Xpath表达式定位元素
	///	
	public JSWebElement findElementByXPath(String using) {
		try {
			return new JSWebElement((RemoteWebElement)we.findElementByXPath(using));
		}catch (NoSuchElementException e){
			return new JSWebElement();
		}
	}

	///
	///通过链接的文字定位元素
	///	
	public JSWebElement findElementByLinkText(String using) {
		try {
			return new JSWebElement((RemoteWebElement)we.findElementByLinkText(using));
		}catch (NoSuchElementException e){
			return new JSWebElement();
		}
	}

	///
	///通过元素DOM表达式定位元素
	///	
	public JSWebElement findElementByDom(String using) {
		try {
			JavascriptExecutor js = this.getJSE();
			WebElement we = (WebElement)js.executeScript(String.format("return %s", using));			
			return new JSWebElement((RemoteWebElement)we);
		}catch (NoSuchElementException e){
			return new JSWebElement();
		}
	}

	///
	///判断元素是否存在
	///
	public Boolean isExist(){
		if (we != null){
			return true;
		}else{
			return false;
		}
	}

	///
	///获取元素的HTML内容
	///
	public String getHtml(){
		return we.getAttribute("outerHTML");
	}

	///
	///获取元素的文本内容
	///	
	public String getText(){
		return we.getText();
	}

	///
	///获取元素的value值
	///	
	public String getValue(){
		return this.getAttribute("value");
	} 
	
	///
	///获取元素的特定属性值
	///
	public String getAttribute(String name){
		return we.getAttribute(name);
	} 
		
	///
	///向可输入元素发送内容,如:text、textarea、filefield等
	///
	public void sendKeys(String string){
		String old_bg = this.setBackground("yellow");
		try {
			Thread.sleep(800);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		we.sendKeys(string);
		this.setBackground(old_bg);
	}
	
	///
	///判断元素是否可用
	///
	public boolean isEnabled(){
		return we.isEnabled();
	}
	
	///
	///判断元素是否可见
	///
	public boolean isVisible(){
		return we.isDisplayed();
	}
	
	///
	///清空可编辑元素的内容。不可编辑元素次操作会抛异常
	///
	public void clear(){
		we.clear();
	}
	
	///
	///对元素进行点击操作
	///
	public void click(){
		we.click();
	}
	
	///
	///检查元素的特定属性值
	///
	public void checkAttr(String attribute, JSWebUtils utils) throws SQLException, JSONException
	{
		String [] attributes=attribute.split("=", 2);
		String actual = this.we.getAttribute(attributes[0]);
		if (actual == null){ actual = "null"; }
		utils.checkPointBase(actual,attributes[1]);
	}
	
	///
	///获取元素的CSS值
	///
	public String getCssValue(String name)
	{
		return we.getCssValue(name);
	}
	
	///
	///判断元素是否被选中
	///
	public boolean isSelected()
	{
		return we.isSelected();
	}	
	
	///
	///可选元素进行选中操作;如:select
	///
	public void select(String by, String value) throws Exception
	{
		if (we.getTagName().equals("select")){
			Select select = new Select(we);
			if (by.equals("index")){
				select.selectByIndex(Integer.parseInt(value));
			}else if (by.equals("value")){
				select.selectByValue(value);
			}else if (by.equals("text")){
				select.selectByVisibleText(value);				
			}
		}else{
			Exception e = new Exception("The element is not SELECT Object");
			throw e;
		}
	}
	
	///
	///对可选中元素进行取消选择操作;如:SELECT in multiple type
	///
	public void deSelect(String by, String...value) throws Exception
	{
		if (we.getTagName().equals("select")){
			Select select = new Select(we);
			if (by.equals("index")){
				select.deselectByIndex(Integer.parseInt(value[0]));
			}else if (by.equals("value")){
				select.deselectByValue(value[0]);
			}else if (by.equals("text")){
				select.deselectByVisibleText(value[0]);
			}else if (by.equals("*")){
				select.deselectAll();
			}
		}else{
			Exception e = new Exception("The element is not SELECT Object");
			throw e;
		}
	}
	
	///
	///判断下拉框是否为多选
	///
	public boolean isMultiple() throws Exception
	{
		if (we.getTagName().equals("select")){
			Select select = new Select(we);
			if (select.isMultiple()){
				return true;
			}else{
				return false;
			}
		}else{
			Exception e = new Exception("The element is not SELECT Object");
			throw e;
		}
	}	
	
	///
	///获取select的当前选中值
	///
	public String getSelectedText() throws Exception
	{
		if (we.getTagName().equals("select")){
			String text = "";
			Select select = new Select(we);
			List<WebElement> options = select.getAllSelectedOptions();
			for (WebElement w : options){
				text += w.getText() + "\r\n";
			}
			return text;
		}else{
			Exception e = new Exception("The element is not SELECT Object");
			throw e;
		}
	}
	
	///
	///判断指定项是否存在
	///
	public boolean isInclude(String name) throws Exception
	{
		if (we.getTagName().equals("select")){
			Select select = new Select(we);
			List<WebElement> options = select.getOptions();
			for (WebElement w : options){
				if (w.getText().equals(name)){
					return true;
				}
			}
			return false;
		}else{
			Exception e = new Exception("The element is not SELECT Object");
			throw e;
		}
	}
	
	///
	///获取元素的tagname
	///
	public String getTagName(){
		return we.getTagName();
	}
	
	///
	///获取元素的id
	///
	public String getId(){
		return we.getId();
	}
	
	///
	///获取元素的绝对位置
	///
	public Point getLocation(){
		return we.getLocation();
	}
	
	///
	///获取元素的出现在屏幕可见区时的位置
	///
	public Point getLocationOnScreenOnceScrolledIntoView(){
		return we.getLocationOnScreenOnceScrolledIntoView();
	}
	
	///
	///获取元素的坐标
	///	
	public Coordinates getCoordinates(){
		return we.getCoordinates();
	}
	
	///
	///获取元素的大小
	///
	public Dimension getSize(){
		return we.getSize();
	}

	///
	///提交元素所在form的内容
	///	
	public void submit()
	{
		we.submit();
	}
	
	///
	///勾选radio、checkbox
	///
	public void check(String...values) throws Exception
	{
		if (we.getTagName().equals("input")){
			if (we.getAttribute("type").equals("radio")){
				WebDriver wd = we.getWrappedDriver();
				List<WebElement> wl = wd.findElements(By.name(we.getAttribute("name")));
				if (values[0].equals("index")){
					wl.get(Integer.parseInt(values[1])).click();
				}else if (values[0].equals("value")){
					for (WebElement w : wl){
						if (w.getAttribute("value").equals(values[1])){
							w.click();
							break;
						}
					}
				}
			}else if (we.getAttribute("type").equals("checkbox")){
				if (!we.isSelected()){
					we.click();
				}	
			}else{
				Exception e = new Exception("The element is not Radio or CheckBox Object");
				throw e;				
			}
		}else{
			Exception e = new Exception("The element is not INPUT Object");
			throw e;
		}
	}
	
	///
	///取消勾选checkbox
	///
	public void unCheck() throws Exception
	{
		if (we.getTagName().equals("input") && we.getAttribute("type").equals("checkbox")){
				if (we.isSelected()){
					we.click();
				}				
		}else{
			Exception e = new Exception("The element is not CheckBox Object");
			throw e;
		}
	}
	
	///
	///checkbox、radio是否勾选
	///
	public boolean isChecked(String...values) throws Exception
	{
		if (we.getTagName().equals("input")){
			if (we.getAttribute("type").equals("radio")){
				WebDriver wd = we.getWrappedDriver();
				List<WebElement> wl = wd.findElements(By.name(we.getAttribute("name")));
				if (values[0].equals("index")){
					return wl.get(Integer.parseInt(values[1])).isSelected();
				}else if (values[0].equals("value")){
					for (WebElement w : wl){
						if (w.getAttribute("value").equals(values[1])){
							return w.isSelected();
						}
					}
				}
				return false;
			}else if (we.getAttribute("type").equals("checkbox")){
				return we.isSelected();
			}else{
				Exception e = new Exception("The element is not Radio or CheckBox Object");
				throw e;				
			}
		}else{
			Exception e = new Exception("The element is not INPUT Object");
			throw e;
		}		
	}

	///
	///把元素滚动到可视区
	///
	public void scroll()
	{
		this.focus();
	}
	
	///
	///高亮元素
	///
	public void highLight() throws InterruptedException
	{
		this.focus();
		JavascriptExecutor js = getJSE();
		String old_style = we.getAttribute("style");
		for (int i = 0; i < 3; i++) { 			 
			js.executeScript("arguments[0].setAttribute(''style'', arguments[1]);", this.we, "background-color: red; border: 2px solid red;" + old_style); 
			Thread.sleep(500);
			js.executeScript("arguments[0].setAttribute(''style'', arguments[1]);", this.we, old_style); 
			Thread.sleep(500);
		}
	}

	///
	///触发元素的特定事件
	///
	public void fireEvent(String event){
		JavascriptExecutor js = getJSE();
		js.executeScript(String.format("arguments[0].%s()", event), this.we);
	}
	
	///
	///使元素获取焦点
	///
	public void focus(){
//		this.we.sendKeys("");
		JavascriptExecutor js = getJSE();
		js.executeScript("arguments[0].focus();", this.we);
	}	
	
	///
	///对元素执行JavaScript操作;即执行元素的dom操作
	///
	public void executeJS(String commands){
		JavascriptExecutor js = getJSE();	
		String[] comandArr = commands.split(";");
		commands = "";
		for (String comand : comandArr){
			if (!comand.trim().equals("")){
				commands += String.format("arguments[0].%s;", comand);
			}
		}
		if (!commands.equals("")){
			js.executeScript(commands, this.we);
		}
	}
	
	///
	///获取原始的RemoteWebElement对象
	///
	public RemoteWebElement getNativeWebElement(){
		return this.we;
	}
		
	private JavascriptExecutor getJSE(){
		if (this.isExist()){
			if (this.jse == null){
				WebDriver wd = we.getWrappedDriver();
				this.jse = (JavascriptExecutor) wd;				
			}		
		}
		return jse;
	}
	
	private String setBackground(String color){
		JavascriptExecutor js = getJSE();
		String old_bg = we.getCssValue("background-color");
		js.executeScript("arguments[0].style.background = arguments[1];", this.we, color); 
		return old_bg;
	}		
	
}

Java Selenium,如何从链接WebElement获取linkText(锚)

Java Selenium,如何从链接WebElement获取linkText(锚)

如何解决Java Selenium,如何从链接WebElement获取linkText(锚)?

试试这个:

System.out.println(link.getText());

解决方法

我有一个包含由url找到的链接的WebElement。我可以通过以下方式提取网址:

element.getAttribute("href");

但是问题是:如何提取它的锚点,我正在尝试这样:

webElement.getAttribute("linkText");

它给了我空值。我100%确信此链接具有锚点。有什么办法获得锚点吗?它更加复杂,但是示例简化的代码可能如下所示:

WebDriver driver = new FirefoxDriver();
        driver.get("http://stackoverflow.com/questions/tagged/java");
        WebElement link  = driver.findElement(By.linkText("Bicycles"));

        System.out.println(link.getAttribute("href")); // shows http://bicycles.stackexchange.com/
        System.out.println(link.getAttribute("linkText")); // shows null

java – WebElement或WebDriver调用findElement方法?

java – WebElement或WebDriver调用findElement方法?

如果我们使用WebElement或WebDriver调用findElement方法有什么区别?他们每个人都有优势吗?提前致谢.

解决方法

WebElement和WebDriver之间的findElement的区别在于上下文.

WebDriver的上下文是当前页面,而WebElement的上下文就是该元素. WebDriver将搜索整个文档,而WebElement将尝试从该节点中查找第一个子元素.

请注意,通过XPath使用WebElement进行搜索时,从//开始仍将搜索整个文档,而不仅仅是当前节点的子项.您可以使用.//将搜索结果限制为该WebElement的子项.

关于如何构建一种方法来遍历WebElements并返回使用Selenium和Java显示的方法?的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于Handle AJAX elements in Selenium 2 (WebDriver)、Java Selenium封装--RemoteWebElement、Java Selenium,如何从链接WebElement获取linkText(锚)、java – WebElement或WebDriver调用findElement方法?的相关信息,请在本站寻找。

本文标签: