如果我在这些标签中有动态更改,如何计算HTML标签?

发布于 01-20 08:07 字数 605 浏览 1 评论 0原文

我想在此页面中计算一些标签 link

我正在尝试为了计算打开位置的标签,所以我尝试了此代码 使用Java计数,但我总是发现我的计数是= 0;

    public By cardsNumOfPositions = By.xpath("//div[@class='card']");
    List<WebElement> element = driver.findElements(cardsNumOfPositions);
    int countelements = element.size();

我写下此功能以计数: -

    public void printCountElements() {
        System.out.println(countelements);
    }

每次计数为0时,我都在搜索一个iframe,但没有找到任何。 那么如何获得此元素的大小呢?

I want to count some tags in this page Link

I am trying to count the tags of opening positions so, I tried this code
using java to count but I always find my count is = 0;

    public By cardsNumOfPositions = By.xpath("//div[@class='card']");
    List<WebElement> element = driver.findElements(cardsNumOfPositions);
    int countelements = element.size();

And I write this function to count:-

    public void printCountElements() {
        System.out.println(countelements);
    }

Everytime the count is 0, I searched for an Iframe but I didn't find any.
so how can I get the size of this element?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蝶舞2025-01-27 08:07:31

另外,您可以将 countelements 设置为静态变量。

像这样:

static int countelements;

现在由于它是静态的,它的价值将持续存在。你可以这样称呼它:

public void printCountElements() {
    countelements = element.size();
    System.out.println(countelements);
}

Also, you can make countelements as a static variable.

like this:

static int countelements;

now since it's static, its value will persist. You can call it like this:

public void printCountElements() {
    countelements = element.size();
    System.out.println(countelements);
}
罪#恶を代价2025-01-27 08:07:31

查看代码,如果您想要实现的是计算页面上与该 xpath 匹配的元素的数量,那么它看起来没有任何问题。

xpath 看起来也不错,我已经测试过它并且它有效。我可以看到页面上存在 iframe,但这些元素看起来并不在这些 iframe 内,因此看起来不需要进入 iframe。

是否有足够的等待时间,以确保在我们尝试找到这些元素之前已完全加载它们?我担心我们试图在加载这些元素之前将其放入列表中。

如果做不到这一点,我接下来要检查的是运行相同的测试,但这次使用该元素的父级(或父级父级),并沿着链跟踪它,看看我们在运行代码时是否可以获得任何命中。

Looking at the code, it doesn't look like there's anything wrong with it if what you're trying to achieve is to count the number of elements on the page matching that xpath.

The xpath also looks good, and I've tested it and it works. I can see that iframes exist on the page, but it doesn't look like these elements are within those iframes, so it doesn't look like stepping into an iframe would be required.

Are there sufficient waits in place, to make sure these elements are fully loaded before we're trying to find them? I'm worried that we're trying to put these elements into a list before they've loaded.

Failing that, what I'd check next is run the same test, but this time using the parents (or parents parents) of this element, and follow it up the chain to see if we can get any hits whilst running the code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文