【问题标题】:Get the list of elements in a div class获取 div 类中的元素列表
【发布时间】:2019-07-29 06:06:39
【问题描述】:

我有多个 div 类。我需要获取文本值的内部 div 元素。

<div data-tab-content-id="2" class="panel active">
  <div class="_1jkaeae">
      <div class="abc-table">
          <div class="aaa nhc">
              <div class="aaa-name">Hello</div>
          </div>
          <div class="aaa mhjss">
              <div class="aaa-name">How</div>
          </div>
      </div>
      <div class="aaa qwqwq">
          <div class="aaa-name">are</div>
      </div>
  </div>
  <div class="aaa kkkk">
      <div class="aaa-name">You</div>
  </div>
</div>
<div class="aaa cccc">
  <div class="aaa-name">doing</div>
</div>
</div>
<div class="aaa cvdsdws">
  <div class="aaa-name">Welcome</div>

</div>
</div>
<div class="aaa klqwq">
  <div class="aaa-name">to the web</div>
</div>
</div>
</div>

从上面的 html 中,我正在寻找的输出是获取所有文本值(Hello,How,are,you,doing,welcome,to the web)。

如果有人可以帮助我,我将不胜感激。

【问题讨论】:

    标签: java html selenium selenide


    【解决方案1】:

    How,are,you,doing,welcome,to the web 它们都共享相同的类名,您可以继续使用 类名css 选择器xpath

    你可以使用这个css选择器:

    .aaa-name
    

    代码:

    driver.findElement(By.cssSelector(".aaa-name")).getText();
    

    Xpath 将是:

    //div[@class='aaa-name']
    

    代码:

    driver.findElement(By.xpath("//div[@class='aaa-name']")).getText();  
    

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      您要查找的文本属于同一个类名。因此,您可以使用className 方法查找文本。
      你可以像这样找到它:

      driver.findElement(By.className("aaa-name")).getText();
      

      【讨论】:

        【解决方案3】:

        由于它们都共享相同的类名,您需要使用类名获取所有元素,即aaa-name,然后循环遍历元素以获得所需的输出。例如:

        List<WebElement> elements = driver.findElements(By.className("aaa-name"));
        for(WebElement element: elements){
            //perform your logic here...
            System.out.println(element.getText());
        }
        

        【讨论】:

          【解决方案4】:

          让我们分步解决问题:

          1. 获取需要文本的所有 div 元素的列表

            List<WebElement> ele_list = driver.findElements(By.xpath("//div[@class='aaa-name']"))
            
          2. 遍历上面的列表并从上面的列表中获取每个元素的文本并打印:

            List<String> text_element = new ArrayList<String>()
            for(WebElement ele : ele_list){
                text_element.add(ele.getText());
            }
            

          希望对你有帮助:)

          【讨论】:

          • 谢谢@Popeye。您的解决方案有效。但我面临另一个问题。得到文本后,我需要断言它们。这是我试过的代码。
          • @newbie 您可以将预期数据保存在列表中,并可以在 for 循环本身中添加断言。例如Assert.assertEquals(ele.getText(), "&lt;expected Text&gt;"); 请在问题本身中添加您尝试断言的代码。让我们看看并尝试找出其中是否有任何问题。
          • @newbie 如果上述答案对您有用,您能否通过单击下方的空心向下箭头将其标记为答案?
          【解决方案5】:

          处理动态元素 Induce WebDriverWaitvisibilityOfAllElementsLocatedBy 并使用以下 css 选择器。

          WebDriverWait wait = new WebDriverWait(driver, 30);
          List<WebElement> elements = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div.aaa-name")));
                  for(int i=0;i<elements.size();i++)
                  {
                      System.out.println(elements.get(i).getText());
          
                  }
          

          控制台输出:

          Hello
          How
          are
          You
          doing
          Welcome
          to the web
          

          【讨论】:

            猜你喜欢
            • 2015-04-18
            • 2018-05-16
            • 2014-09-17
            • 2012-11-02
            • 2020-08-02
            • 2010-11-16
            • 1970-01-01
            • 2012-04-04
            • 1970-01-01
            相关资源
            最近更新 更多