本文介绍了如何在一格内从不同元素获取所有文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,这是html伪代码:

I have problem, here is html pseudocode:

<div class="content" id="ui6" role ="tabpanel">
 <p> text </p>
 <ul> <li> text2 </li>
      <li> text3 </li>
 </ul>
  </div>

我想获取text,text2和text3并打印它们,但我只能设法获取"text".

I want to get text, text2 and text3 and print them, but I can only manage to get "text".

这是代码:

List<WebElement> sectionThreeText = driver.findElements(By.cssSelector("#ui6"));
    for(WebElement e : sectionThreeText) {
        System.out.println(e.getText());
    }

我也尝试通过以下方式找到它:

I tried also find it by:

css = "#ui6 > p ul"
xpath = "//*[@id=ui6]"
xpath = "//*[@id=ui6]/*"
xpath = "//*[@id=ui6]/p | //*[@id=ui6]/ul"

我没主意了.我在哪里弄错了?

I am out of ideas. Where I make mistake?

推荐答案

要打印 text text2 text3 ,您可以使用以下任一方法以下定位器策略:

To print text, text2 and text3 you can use either of the following Locator Strategies:

  • cssSelector :

System.out.println(driver.findElement(By.cssSelector("div.content#ui6")).getText());

  • xpath :

    System.out.println(driver.findElement(By.xpath("//div[@class='content' and @id='ui6']")).getText());
    

  • 或者,您也可以使用以下定位器策略:

    As an alternative you can also use the following Locator Strategies:

    • 要打印文本:

    • cssSelector :

    System.out.println(driver.findElement(By.cssSelector("div.content#ui6>p")).getText());
    

  • xpath :

    System.out.println(driver.findElement(By.xpath("//div[@class='content' and @id='ui6']/p")).getText());
    

  • 要打印 text2 :

    • cssSelector :

    System.out.println(driver.findElement(By.cssSelector("div.content#ui6 ul>li")).getText());
    

  • xpath :

    System.out.println(driver.findElement(By.xpath("//div[@class='content' and @id='ui6']//ul/li")).getText());
    

  • 要打印 text3 :

    • cssSelector :

    System.out.println(driver.findElement(By.cssSelector("div.content#ui6 ul li:nth-child(2)")).getText());
    

  • xpath :

    System.out.println(driver.findElement(By.xpath("//div[@class='content' and @id='ui6']//ul//following-sibling::li[2]")).getText());
    

  • 这篇关于如何在一格内从不同元素获取所有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    11-01 04:52