【问题标题】:Web-table elements not present using selenium webdriver?使用 selenium webdriver 不存在的 Web 表元素?
【发布时间】:2017-07-13 15:24:14
【问题描述】:

我有一种情况,我不需要处理这样的元素存在异常,下面是场景。

1.客户已注册 2.客户未注册 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);
            }

新客户:

Customer web-table grid details中不会显示这样的详细信息。它是空的,我需要注册这个新客户。请提供解决方案如何为此编写条件。

空时

<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>

【问题讨论】:

  • 在这两种情况下分享HTML
  • Hi Andersson,请在问题详情中找到 HTML 代码

标签: selenium xpath


【解决方案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);
            }

【讨论】:

  • 嗨,Murthi,非常棒,它按预期工作,非常感谢。
猜你喜欢
  • 2014-07-18
  • 1970-01-01
  • 2015-05-29
  • 1970-01-01
  • 2015-09-01
  • 2023-04-01
  • 2011-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多