使用selenium webdriver不存在的Web表元素?

时间:2017-07-13 06:43:46

标签: selenium xpath

我有一种情况,我需要处理没有这种元素存在的异常,下面就是这种情况。

1.客户已注册 2.Customer未注册 3.新鲜客户

对于注册客户,我可以使用xpath获取注册卡号,但是新客户在Web表格中没有显示此类客户详细信息,在这种情况下我的条件不起作用并且不显示此类元素。

例如:

注册客户

客户详细信息网表格中的移动号码,名字,姓氏,卡号等

String card_no=driver.findElement(By.xpath("//td[@aria-describedby='CustomerGrid_cardNo']")).getText();
             System.out.println(card_no);

            if(card_no.length()==0){
                System.out.println("Customer is not register and proceeding for registration");
                driver.findElement(By.id("cardNo")).sendKeys("39409297");
                driver.findElement(By.id("Btn")).click();
                Thread.sleep(2500);             

            }else{
                System.out.println("Customer Mobile No is already registered -"+card_no);
            }

新客户:

没有这样的细节会显示在客户网络表网格详细信息中。它是空的,我需要注册这个新客户。请提供解决方案如何为此写条件。

空白时

<table width="470" cellspacing="0" cellpadding="0" border="0" align="center">
<tbody>
<tr>
<tr>
<td>
<div id="gbox_CustomerGrid" class="ui-jqgrid ui-widget ui-widget-content ui-corner-all" dir="ltr" style="width: 570px;">
<div id="lui_CustomerGrid" class="ui-widget-overlay jqgrid-overlay"></div>
<div id="load_CustomerGrid" class="loading ui-state-default ui-state-active" style="display: none;">Loading...</div>
<div id="gview_CustomerGrid" class="ui-jqgrid-view" style="width: 570px;">
<div class="ui-jqgrid-titlebar ui-widget-header ui-corner-top ui-helper-clearfix" style="display: none;">
<div class="ui-state-default ui-jqgrid-hdiv" style="width: 570px;">
<div class="ui-jqgrid-bdiv" style="height: 300px; width: 570px;">
<div style="position:relative;">
<div></div>
<table id="CustomerGrid" class="ui-jqgrid-btable" cellspacing="0" cellpadding="0" border="0" tabindex="1" role="grid" aria-multiselectable="false" aria-labelledby="gbox_CustomerGrid" style="width: 552px;">
</div>
</div>
</div>

包含数据:

<table id="CustomerGrid" class="ui-jqgrid-btable" cellspacing="0" cellpadding="0" border="0" tabindex="1" role="grid" aria-multiselectable="false" aria-labelledby="gbox_CustomerGrid" style="width: 552px;">
<tbody>
<tr class="jqgfirstrow" style="height:auto" role="row">
<td aria-describedby="CustomerGrid_cardNo" title="7348054" style="text-align:center;" role="gridcell">7348054</td>
</tr>

1 个答案:

答案 0 :(得分:1)

您可以尝试如下,

int card_no=driver.findElements(By.xpath("//td[@aria-describedby='CustomerGrid_cardNo']")).size();
             System.out.println(card_no);

            if(card_no==0){
                System.out.println("Customer is not register and proceeding for registration");
                driver.findElement(By.id("cardNo")).sendKeys("39409297");
                driver.findElement(By.id("Btn")).click();
                Thread.sleep(2500);             

            }else{
                System.out.println("Customer Mobile No is already registered -"+card_no);
            }