单击具有相同类selenium的动态复选框

时间:2017-02-14 11:44:34

标签: c# selenium dynamic webdriver elements

我正在使用selenium webdriver和c#进行自动化项目。

现在我有一个挑战需要解决:我的系统是一个支付门票的系统,每张门票都是动态的,带有组件的页面,支付它需要点击动态复选框(每张门票都有复选框)。下面是一个有一些支付票的例子:

<form action="/PagamentoOnline/fix_3001837_conexaoCore/Pagamento/FormaPagamento" id="frmDebitos" method="post"><input data-val="true" data-val-required="The TipoPagamento field is required." id="TipoPagamento" name="TipoPagamento" type="hidden" value="0" />    <div class="row">
    <div class="col-md-6 txt-gray txt-right"><input data-val="true" data-val-required="The TipoSolicitacaoPagamento field is required." id="TipoSolicitacaoPagamento" name="TipoSolicitacaoPagamento" type="hidden" value="Pagamento" /></div>
    <div class="col-md-3 txt-gray txt-right">
        <input id="DataPagamento" name="DataPagamento" type="hidden" value="2017-02-13">
        <span class="huge">13/02/2017</span>
        <br />
        <span class="medium">Data de pagamento</span>
    </div>
    <div class="col-md-3 txt-gray txt-right">
        <span class="huge total-debitos-selecionados"></span>
        <br />
        <span class="medium">Total de débitos</span>
    </div>
</div>
<hr />
<div class="row">
        <div class="col-md-4 col-debito">
            <input class="chk-debitos-selecionados" id="DebitosSelecionados_0_" name="DebitosSelecionados[0]" type="checkbox" value="29133609" /><input name="DebitosSelecionados[0]" type="hidden" value="false" />
            <div class="panel panel-gray"
                 data-codigo-titulo="29133609"
                 data-valor-pagar="21.41"
                 data-valor-original="20"
                 data-data-vencimento="Venc.: 15/12/2016"
                 data-numero-parcela="1"
                 data-descricao-tipo-debito="Parcela"
                 data-sigla-periodo-letivo="2016/2">
                <div class="panel-heading">
                    <div class="row">
                        <div class="col-xs-12">
                            <div class="medium pull-right"><i class="glyphicon glyphicon-unchecked"></i></div>
                            <div class="huge pull-left" data-bind="text: valor">R$ 21,41</div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-xs-12">
                            <div class="pull-left">Valor Atualizado</div>
                            <div class="pull-right">1&#170; Parcela - 2016/2</div>
                        </div>
                    </div>
                </div>
                <div class="panel-footer">
                    <span class="pull-left">D&#233;bito: R$ 20,00</span>
                    <span class="pull-right">Venc.: 15/12/2016</span>
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>
        <div class="col-md-4 col-debito">
            <input class="chk-debitos-selecionados" id="DebitosSelecionados_1_" name="DebitosSelecionados[1]" type="checkbox" value="29608740" /><input name="DebitosSelecionados[1]" type="hidden" value="false" />
            <div class="panel panel-gray"
                 data-codigo-titulo="29608740"
                 data-valor-pagar="21.43"
                 data-valor-original="20.99"
                 data-data-vencimento="Venc.: 19/01/2017"
                 data-numero-parcela="1"
                 data-descricao-tipo-debito="Parcela"
                 data-sigla-periodo-letivo="2016/2">
                <div class="panel-heading">
                    <div class="row">
                        <div class="col-xs-12">
                            <div class="medium pull-right"><i class="glyphicon glyphicon-unchecked"></i></div>
                            <div class="huge pull-left" data-bind="text: valor">R$ 21,43</div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-xs-12">
                            <div class="pull-left">Valor Atualizado</div>
                            <div class="pull-right">1&#170; Parcela - 2016/2</div>
                        </div>
                    </div>
                </div>
                <div class="panel-footer">
                    <span class="pull-left">D&#233;bito: R$ 20,99</span>
                    <span class="pull-right">Venc.: 19/01/2017</span>
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>
        <div class="col-md-4 col-debito">
            <input class="chk-debitos-selecionados" id="DebitosSelecionados_2_" name="DebitosSelecionados[2]" type="checkbox" value="29675826" /><input name="DebitosSelecionados[2]" type="hidden" value="false" />
            <div class="panel panel-gray"
                 data-codigo-titulo="29675826"
                 data-valor-pagar="1210.42"
                 data-valor-original="1180.95"
                 data-data-vencimento="Venc.: 07/02/2017"
                 data-numero-parcela="2"
                 data-descricao-tipo-debito="Parcela"
                 data-sigla-periodo-letivo="2017/1">
                <div class="panel-heading">
                    <div class="row">
                        <div class="col-xs-12">
                            <div class="medium pull-right"><i class="glyphicon glyphicon-unchecked"></i></div>
                            <div class="huge pull-left" data-bind="text: valor">R$ 1.210,42</div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-xs-12">
                            <div class="pull-left">Valor Atualizado</div>
                            <div class="pull-right">2&#170; Parcela - 2017/1</div>
                        </div>
                    </div>
                </div>
                <div class="panel-footer">
                    <span class="pull-left">D&#233;bito: R$ 1.180,95</span>
                    <span class="pull-right">Venc.: 07/02/2017</span>
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>
</div>
<hr />
<div class="row">
    <div class="col-md-7"></div>
        <div class="col-md-2 top-right">
        </div>
    <div class="col-md-1"></div>
    <div class="col-md-2 top-right">
        <button id="PagamentoCredito" type="submit" class="btn btn-success">Pagar com cartão de credito</button>
    </div>
</div>

每个div类:

<div class="col-md-4 col-debito"> 

是支付的门票。 我需要选择其中一张票并选中复选框

<i class="glyphicon glyphicon-unchecked"></i>

我尝试使用foreach循环来选择一个特定的票证复选框:

ticketToPay = "cod_ticket_to_pay";
ReadOnlyCollection<IWebElement> ticketsInSystem= driver.FindElements(By.XPath("//div[@class='panel panel-gray']"));


foreach (IWebElement ticketInformation in ticketsInSystem)
{
if (ticketInformation.GetAttribute("data-codigo-titulo").Equals(ticketToPay))
  {
IWebElement checkBoxTicket = ticketInformation.FindElement(By.XPath("//*[@class='glyphicon glyphicon-unchecked']"));
checkBoxTicket .Click();
}
}

但是当元素 checkboxTicket 的操作是单击其类型的第一个元素时,而不是正确的票证。有谁知道如何解决这个问题?

一张票 - 一张复选框选择的票证 - 选择复选框以点击。

1 个答案:

答案 0 :(得分:0)

我不知道如何用c#做这个。但是使用Java语言你可以这样做。

WebElement check1 = driver.findElement(By.xpath("//input[@value='29133609']"));
WebElement check2 = driver.findElement(By.xpath("//input[@value='29608740']"));
WebElement check3 = driver.findElement(By.xpath("//input[@value='29675826']"));

int[] check_values = new int[] {29133609, 29608740, 29675826};

for(int i =0; i<check_values.length; i++)
{
    WebElement check_source = driver.findElement(By.xpath("//input[@value='"+check_values[i]+"']"));
    if(!check_source.isSelected())
    {
        check_source.click();
        Thread.sleep(2500);
    }
}