简体   繁体   English

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

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

I am trying to get the total number of items in an HTML page by a given class. 我正在尝试通过给定的类获取HTML页面中的项目总数。 I am using Selenium and Autohotkey to do it 我正在使用Selenium和Autohotkey来做到这一点

I've searched a lot of this topic but haven't found my particular solution 我已经搜索了很多这个主题,但是还没有找到自己的解决方案

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

Using this code to work with: 使用此代码可用于:

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

Output: Blueberry 输出:蓝莓

Now, for getting a certain item of a class using Xpath, is as follows: 现在,为了使用Xpath获取类的某个项,如下所示:

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

Output: Strawberry 输出:草莓

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" ) 所以我需要一个输出给我“ 4 ”(因为有4个“ buttons”

I havent find a way to do this in Autohotkey. 我还没有办法在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 C

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

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

and more but this doesn't work with Autohokey because of those functions and variables (like len(), IList, and others) 等等,但是由于这些函数和变量(例如len(),IList等),这不适用于Autohokey

I am expecting to get just the total number of items by any way possible 我希望以任何可能的方式获得 项目总数

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) 我正在考虑尚未建立的Selenium的某些功能,但我不知道(例如某些-在行的末尾-“ .len”,“。size”,“。count”,但它们都不起作用为了我)

Any suggestions are welcome and appreciated, thanks! 任何建议都值得欢迎和赞赏,谢谢!

EDIT: wow I was just missing the " () " on " .Count " 编辑:哇,我只是在“ .Count ”上缺少“ ()

this is what I was looking for 这就是我想要的

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

Thanks to supputuri 感谢supputuri

You can use Count() method to get the number of elements matching your xpath. 您可以使用Count()方法获取与xpath匹配的元素数。

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

you can refer to this for more information. 您可以参考此以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM