如何使用Selenium WebDriver和Autohotkey获取HTML中的元素总数? [英] How to get the total number of elements in a HTML with Selenium WebDriver and Autohotkey?

查看:180
本文介绍了如何使用Selenium WebDriver和Autohotkey获取HTML中的元素总数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过给定的类获取HTML页面中的项目总数.我正在使用Selenium和Autohotkey

I am trying to get the total number of items in an HTML page by a given class. I am using Selenium and Autohotkey to do it

我已经搜索了很多这个主题,但是还没有找到自己的解决方案

I've searched a lot of this topic but haven't found my particular solution

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

Most of suggestions and answers I researched included solutions but for Java or other language, not for Autohotkey (Although they have similar structures in this case)

使用此代码进行处理:

<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>

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

For getting the text from a element by class it can be done by this:

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

输出:蓝莓

现在,要使用Xpath获取类的某个项,如下:

Now, for getting a certain item of a class using Xpath, is as follows:

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

输出:草莓

我需要获取按钮"的总数.因此,我需要一个输出,该输出为我提供" 4 "(因为有 4个按钮" )

What I need is to get the total number of "buttons". So I need an output that gives me "4" (since there is 4 "buttons")

我还没有办法在Autohotkey中做到这一点.我见过针对其他语言的解决方案,例如

I havent find a way to do this in Autohotkey. I've seen solutions for other languages like

A

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);
}

更多,但由于这些函数和变量(例如len(),IList等),这不适用于Autohokey

and more but this doesn't work with Autohokey because of those functions and variables (like len(), IList, and others)

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

I am expecting to get just the total number of items by any way possible

我正在考虑尚未建立的Selenium的某些功能,我也不知道(例如某些-在行的末尾-".len",.size",.count",但不是其中有为我工作的人)

I am thinking on maybe some function for Selenium I haven't founded yet and I dont know about (like some -at the end of the line- ".len",".size",".count" but non of them worked for me)

任何建议都值得欢迎和感谢,谢谢!

Any suggestions are welcome and appreciated, thanks!

哇,我只是错过了 .Count "

这就是我想要的

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

感谢化脓

推荐答案

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

You can use Count() method to get the number of elements matching your xpath.

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

您可以参考以获取更多信息.

you can refer to this for more information.

这篇关于如何使用Selenium WebDriver和Autohotkey获取HTML中的元素总数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆