有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

我想计算特定文本webdriver java在多个页面上可用的HTML表中的数据

  • 我正在尝试计算行的总数
  • 我需要统计不同类型的可用状态(非活动、活动、过期和耗尽)

    当前代码仅正在获取:

    • 行数
    • 状态和名称的文本,但我只需要写在状态类型旁边的计数
    • 它只从第一页获取数据。页面根据事务数填充

    下面是编写的代码:

    import java.util.List;
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.By;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class counting {
      public static void main(String[] args) {
        String Status="Inactive";
        //String NextPage="Inactive";
        WebDriver driver= new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.navigate().to("http://Testsite/web");
        driver.findElement(By.id("UserEntry")).sendKeys("00"); 
    
          //Type Last Name
    
        driver.findElement(By.id("Password")).sendKeys("Test");
    
           // Click on Submit
    
        driver.findElement(By.id("LoginButton")).click();
    
        driver.findElement(By.linkText("Accounts"));
    
        driver.findElement(By.linkText("Accounts")).click();
        driver.findElement(By.xpath("html/body/form/div[3]/div/div[2]/table /tbody/tr/td[1]/div/ul/li[2]/a")).click();
    
        List<WebElement> Account =  driver.findElements(By.xpath("//table[@id='MainPlaceholder_AccountList']/tbody/tr/td[1]"));
        List<WebElement> AccountStatus = driver.findElements(By.xpath("//table[@id='MainPlaceholder_AccountList']/tbody/tr/td[5]"));
        System.out.println("Total Account- "+Account.size());
        System.out.println("Total Status- "+AccountStatus.size());
    

共 (2) 个答案

  1. # 1 楼答案

      for(int i=0;i<AccountStatus.size();i++){
                    if(AccountStatus.get(i).getText().equals(Status))
                    System.out.println(Account.get(i).getText()+"  -    "+AccountStatus.get(i).getText());
    
            }
    
        }
    
    }
    
    
        HTML COde:
    
       <table id="MainPlaceholder_AccountList" cellspacing="1" cellpadding="10"    border="1" style="background-color:#888888;width:100%;font-size: 75%; padding: 5px;" rules="all">
        <tbody>
        <tr align="center" style="color:White;background-color:#232356;">
        <th scope="col">Account Name</th>
        <th scope="col">Number</th>
      <th scope="col">Program</th>
      <th scope="col">Site</th>
      <th scope="col">Status</th>
      <th scope="col">Edit</th>
      <th scope="col">Delete</th>
      </tr>
      <tr style="background-color:White;">
       <td class="capText" style="width:220px;">Last, First</td>
         <td class="padIt" align="center" style="width:60px;">4563201</td>
      <td class="capText" style="width:150px;"> </td>
      <td class="padIt" style="width:115px;">local</td>
      <td class="padIt" style="width:90px;">Active</td>
      <td style="width:75px;">
      <td style="width:75px;">
      <td style="width:75px;">
       <input id="MainPlaceholder_AccountList_EditAccount_0" type="submit"   style="width:75px;" eid="550" value="Edit..." name="ctl00$MainPlaceholder$AccountList$ctl02$EditAccount">
    </td>
      <td style="width:75px;">
     <input id="MainPlaceholder_AccountList_DeleteAccount_0" type="submit" style="width:75px;" eid="550" onclick="return confirm('Are you sure you want to delete the selected account?');" value="Delete" name="ctl00$MainPlaceholder$AccountList$ctl02$DeleteAccount">
    </td>
    
      td style="text-align: center">
        <span id="MainPlaceholder_PageLabel" style="margin-right: 5px;">Page:</span>
      <input id="MainPlaceholder_CurrentPage" type="text" style="width:40px;text-align: right" onclick="javascript:this.value="";" title="Type in a page number, then press the Enter key to go directly to that page." maxlength="5" value="1" name="ctl00$MainPlaceholder$CurrentPage">
      <span id="MainPlaceholder_OfLabel" style="margin-left: 5px; margin-right: 5px;">of</span>
      <span id="MainPlaceholder_TotalPages" style="margin-right: 5px;">21</span>
        <input id="MainPlaceholder_LastButton" type="submit" style="height:24px;width:90px;" value="▼▼ Last" name="ctl00$MainPlaceholder$LastButton">
      <input id="MainPlaceholder_NextButton" type="submit" style="height:24px;width:90px;" value="▼ Next" name="ctl00$MainPlaceholder$NextButton">
        <input id="MainPlaceholder_PreviousButton" type="submit" style="height:24px;width:90px;" value="Previous ▲" name="ctl00$MainPlaceholder$PreviousButton">
        <input id="MainPlaceholder_FirstButton" type="submit" style="height:24px;width:90px;" value="First ▲▲" name="ctl00$MainPlaceholder$FirstButton">
    </td>
    
  2. # 2 楼答案

    你的代码没有完全正确格式化。我在移动测试中使用了Appium(所以它是selenium后端),我假设您可以尝试创建一个到达某个点的表,然后创建由某个类名(或者在您的情况下,我假设是一个id值)组成的行

    例:

    table = (MobileElement) driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[2]/UIATableView[1]"));
    rows = table.findElementsByClassName("UIATableCell");
    

    司机是一名司机。这就是我为iOS测试定位某些元素的方式。但我还是要假设逻辑是一样的。一旦你找到了你需要的东西,就用这些对象填充你的数组

    一旦你填充了你的数组,你可能会解析它来满足你的需要