有 Java 编程相关的问题?

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

Selenium java代码,用于查找网页上H2标记的编号

请建议一个代码来查找网页上H2标记的总数

下面是我的代码,但输出不正确

要检查的URL:http://www.marksandspencer.com/l/lingerie/sexy-lingerie

public class H1andh2 {
  WebDriver driver;
    @BeforeTest
    public void beforeTest(){
        driver= new FirefoxDriver();
    }


  @Test(priority=2)
  public void pid(){

        driver.get("http://www.marksandspencer.com/l/lingerie/sexy-lingerie");
        List<WebElement> h1= driver.findElements(By.tagName("h1"));
        System.out.println("number of H1 tags are:"+ h1.size());
        List<WebElement> h2= driver.findElements(By.tagName("H2"));
        System.out.println("number of H2 tags are:"+ h2.size());


    }
}

共 (1) 个答案

  1. # 1 楼答案

    页面可能尚未完全加载。虽然已加载,但JavaScript可能仍在添加元素。如果h2元素深深嵌入到HTML中,那么可以在路径中找到更高的元素,然后在中间WebElement上查找h2标记(缩短搜索;您不希望搜索在到达h2标记之前超时)