如何使用 Selenium WebDriver 和 Autohotkey 获取 HTML 中的元素总数?

How to get the total number of elements in a HTML with Selenium WebDriver and Autohotkey?

我正在尝试通过给定的 class 获取 HTML 页面中的项目总数。我正在使用 Selenium 和 Autohotkey 来做到这一点

我搜索了很多这个主题,但没有找到我的特定解决方案

我研究的大多数建议和答案都包含解决方案,但针对 Java 或其他语言,而不是针对 Autohotkey(尽管在这种情况下它们具有相似的结构)

将此代码用于:

<html>
    <body>
        <div id="pancakes">
            <button class="button">Blueberry</button><br><br>
            <button class="button">Banana</button><br><br>
            <button class="button">Strawberry</button><br><br>
            <button class="button">Yumi</button><br><br>
        </div>
    </body>
</html>

要通过 class 从元素中获取文本,可以这样做:

driver.findElementByClass("button").Attribute("innerText")

输出:蓝莓

现在,使用Xpath获取class的某一项,如下:

driver.findElementsByXpath("//*[contains(@id,'pancakes')]/button").item[1].Attribute("innerText") 

输出:草莓

我需要的是得到"buttons"的总数。所以我需要一个 output 给我“4”(因为有 4 "buttons")

我还没有找到在 Autohotkey 中执行此操作的方法。我看过其他语言的解决方案,例如

一个

len(driver.find_elements_by_xpath('//a'))

B

WebElement webElement = driver.findElement(By.xpath("//form[@id='form1']/div[4]"));

//Get list of table elements using tagName
List<WebElement> list = webElement.findElements(By.tagName("table"));

C

IList<IWebElement> selectElements = driver.FindElements(By.TagName("select"));

foreach (IWebElement select in selectElements)
{
    var selectElement = new SelectElement(select);
    Console.WriteLine(selectElement.SelectedOption.Text);
}

等等,但这不适用于 Autohokey,因为这些函数和变量(如 len()、IList 和其他)

我希望通过任何可能的方式得到项目总数

我正在考虑一些我尚未创建并且不知道的 Selenium 功能(比如一些 - 在该行的末尾 - “.len”,“.size”,“.count”但没有一个对我有用)

欢迎和赞赏任何建议,谢谢!

编辑:哇,我只是错过了“.Count

上的“()

这就是我要找的东西

driver.findElementsByXpath("//*[contains(@id,'pancakes')]/button").Count()

感谢 supputuri

您可以使用 Count() 方法获取与您的 xpath 匹配的元素数量。

 driver.findElementsByXpath("//*[contains(@id,'pancakes')]/button").Count()

您可以 refer 了解更多信息。