如何检查硒中面包屑的顺序

How to check the order of breadcrumbs in selenium

我有一个 header 定义为第一/第二/第三作为面包屑。我想使用具有最佳编码方式的 selenium 检查元素是否以正确的顺序显示。

<ol class = "breadcrumb"
     <li class="break-all">        
          <a href="..." class="break-all">First</a>
          <span class="divider">/</span> 
   </li>
   <li class="break-all">        
          <a href="..." class="break-all">Second</a>
          <span class="divider">/</span> 
   </li>
   <li class="break-all">        
          <a href="..." class="break-all">Third</a>
          <span class="divider">/</span> 
   </li>
</ol>

现在当我

findBy("//ol[@class='breadcrumb']")

,我得到了全部元素。

首先,您需要找到所有面包屑元素,例如,通过 cssSelector()。然后,对于列表中的每个 WebElement 调用 getText() 以获取实际文本:

List<String> expected = Arrays.asList("First", "Second", "Third");

List<WebElement> breadcrumbs = driver.findElements(By.cssSelector("ol.breadcrumb li a"));
for (int i = 0; i < expected.length; i++) {
    String breadcrumb = breadcrumbs.get(i).getText();
    if (breadcrumb.equals(expected[i])) {
        System.out.println("passed on: " + breadcrumb);
    } else {
        System.out.println("failed on: " + breadcrumb);
    }
}

您需要创建一个已知值列表以进行比较。然后使用 findElements() 查找所有元素以匹配您的目标。因为您还需要仔细编写选择器,以便它获取预期元素的列表。 //a[@class='break-all']可以用来抓取你想要的元素列表

String[] expected = {"First", "Second", "Third"};
List<WebElement> allOptions = select.findElements(By.xpath("//a[@class='break-all']"));

// make sure you found the right number of elements
if (expected.length != allOptions.size()) {
    System.out.println("fail, wrong number of elements found");
}
// make sure that the value of every <option> element equals the expected value
for (int i = 0; i < expected.length; i++) {
    String optionValue = allOptions.get(i).getText();
    if (optionValue.equals(expected[i])) {
        System.out.println("passed on: " + optionValue);
    } else {
        System.out.println("failed on: " + optionValue);
    }
}

实施取自here

要解决您的问题,您可以按照以下流程:

1-创建一个"ArrayList"并添加所有需要与之比较的项目。
2- 检索 link 文本并将其放入新的 ArrayList。
3- 断言两个 ArrayList 匹配。

以下代码适用于您:

//Adding all the list items to compare in an ArrayList
ArrayList<String> alist = new ArrayList<String>();
alist.add("First");
alist.add("Second");
alist.add("Third");

//Checking the Arraylist's data
System.out.println("The list values are as under: ");
for(String list_item: alist)
    System.out.println(list_item);

//Creating an ArrayList to store the retrieved link texts
ArrayList<String> List_Compare = new ArrayList<String>();

//Retrieving the link texts and putting them into the Arraylist so created
List<WebElement> New_List = driver.findElements(By.xpath("//a[@class='break-all']"));
for(WebElement list_item: New_List){
    List_Compare.add(list_item.getText());
}

//Checking the new Arraylist's data
System.out.println("The Retrieved list values are as under: ");
for(String list_item: List_Compare)
    System.out.println(list_item);

//Asserting the original Arraylist matches to the Arraylist with retrieved Link Texts 
try{
    Assert.assertEquals(alist, List_Compare);
    System.out.println("Equal lists");
}catch(Throwable e){
    System.err.println("Lists are not equal. "+e.getMessage());
}

注意: 使用 import junit.framework.Assert;[=26 导入 Assert class =] 使代码的最后一部分起作用。

findBy("//ol[@class='breadcrumb']").getText().equals("First / Second / Third");

应该可以。