无法从硒中的输入元素获取文本

时间:2019-02-04 10:57:40

标签: java html spring selenium

使用Java / Spring标记,我编写了一个html文件,其中包含一些从数据库获取的单选按钮。现在,我正在编写Selenium测试用例以单击该单选按钮。当我使用Excel工作表读取值时,我的单选按钮值在Excel工作表中存在。因此,如何在excel中使用文本识别单选按钮,然后从Selenium WebDriver中单击它。

我尝试使用Xpath并遍历WebElement并使用getText()方法仍然会获取空Text。

实际HTML:

<html>
<div class="col-xs-6 col-md-6 prop_value">
<c:forEach items="${products}" var="productTemp">
<form:radiobutton style="margin-left : 30px"                             
path="${mtool.productDetail.productId}" class="radiobtn"                                             
name="product" value="${productTemp.productId}"/>${productTemp.productName} 
</c:forEach></div>
</html>

现在,当我在浏览器中检查此HTML时,生成的情况如下

检查的HTML:

<div class="col-xs-8 col-md-8 prop_title">
Product<span class="required">*</span>
<input id="1" name="product" style="margin-left : 30px" class="radiobtn" 
type="radio" value="1">CAR
<input id="2" name="product" style="margin-left : 30px" class="radiobtn" 
type="radio" value="2">BUS
<input id="3" name="product" style="margin-left : 30px" class="radiobtn" 
type="radio" value="3">TRUCK
<input id="4" name="product" style="margin-left : 30px" class="radiobtn" 
type="radio" value="4">TRAIN
<input id="5" name="product" style="margin-left : 30px" class="radiobtn" 
type="radio" value="5">AEROPLANE
</div>

这些值CAR,BUS,TRUCK,TRAIN,AEROPLANE存储在Excel工作表中。现在,基于Excel工作表中存储的值单击单选按钮,我编写了Selenium Code如下。

Example.java

for(int i=0; i<totalRows; i++){
        cell = sheet.getRow(i).getCell(0);
        System.out.println(driver.findElement(By.xpath("//div[@class='col-xs-6 col-md-6 prop_value']")).getText());
        List<WebElement> elmnt=driver.findElements(By.xpath("//input[@name='product' and @class='radiobtn']/following-sibling::text()/parent::div/input"));
        for(WebElement list : elmnt){

            String text=list.getText();
            System.out.println("Text"+text);
            if(text.equalsIgnoreCase(cell.getStringCellValue())){
                list.click();
            }
        }

这只是一个仅与单选按钮相关的代码,因此我没有编写完整的类。现在,我的文本为空,因此未单击单选按钮。所以有人可以让我知道该怎么做。谢谢

5 个答案:

答案 0 :(得分:0)

使用此xpath: List<WebElement> elmnt=driver.findElements(By.xpath("//input[@name='product']"));

答案 1 :(得分:0)

您可以尝试使用innerText:

String text=list.getAttribute("innerText");

答案 2 :(得分:0)

您可以使用其他方法进行操作。如果您愿意,它会给您整个'div'文本值。然后您可以拆分该值并与excel shell值进行比较。

//Radio Button element 
List<WebElement> elmnt = driver.findElements(By.xpath("//div[text()[contains(.,'')]]/input"));
//It returns Entire Div Text  
String[] parts =driver.findElement(By.xpath("//input[@name='product']/parent::div")).getText().split(" ");

for (int i=1;i<parts.length;i++)
    {
          System.out.println(parts[i]);
           elmnt.get(i-1).click();
           Thread.sleep(2000); //sleep i have used for testing purpose not required in real scenario.
    }

答案 3 :(得分:0)

根据您的要求,我将发布另一个单元格答案。

for(int j=0; j<totalRows; j++){
         cell = sheet.getRow(j).getCell(0); 
        //Radio Button element 
         List<WebElement> elmnt = driver.findElements(By.xpath("//div[text()[contains(.,'')]]/input"));
         //It returns Entire Div Text  
         String[] parts =driver.findElement(By.xpath("//input[@name='product']/parent::div")).getText().split(" ");

         for (int i=1;i<parts.length;i++)
             {
                    System.out.println(parts[i]);
                    //Verifying cell value here.
                    if(parts[i].trim().equalsIgnoreCase(cell.getStringCellValue()))
                    {
                        elmnt.get(i-1).click();
                    }

              }

         }

请让我知道那是您要照顾的?

答案 4 :(得分:0)

您可以在下面使用xpath获取输入,但最好让开发人员添加关闭标签:

//node()[normalize-space(.)='CAR']/preceding::input[1]

//node()[normalize-space(.)='AEROPLANE']/preceding::input[1]
相关问题