I am currently working on a project that requires me to determine the total number of items in an HTML page based on a specified class. My tools of choice for this task are Selenium and Autohotkey.
Despite extensive research on the topic, I have yet to find a solution that fits my specific needs.
Most of the suggestions and answers I came across were geared towards Java or other programming languages, rather than Autohotkey (even though they share similar structures).
Here is the code snippet I am working 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>
To extract text from an element based on its class using Autohotkey, you can use the following code snippet:
driver.findElementByClass("button").Attribute("innerText")
Output: Blueberry
Now, if you wish to retrieve a specific item within a class using XPath, you can do so by:
driver.findElementsByXpath("//*[contains(@id,'pancakes')]/button").item[1].Attribute("innerText")
Output: Strawberry
The goal here is to obtain the total count of "buttons". Therefore, the desired output should be "4" (as there are 4 "buttons" present)
Unfortunately, I have been unable to find a suitable method to achieve this in Autohotkey. While solutions exist 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);
}
such functions and variables including len(), IList, among others, are not compatible with Autohotkey.
I am simply seeking a way to obtain the total number of items through any means possible.
One potential avenue I am considering is the existence of a Selenium function that has eluded me thus far, such as one ending in ".len", ".size", ".count," but none of these have proven effective for me.
Any advice or recommendations would be greatly appreciated. Thank you!
EDIT: It turns out, I just forgot the "()" when using ".Count"
This was the missing piece of the puzzle:
driver.findElementsByXpath("//*[contains(@id,'pancakes')]/button").Count()
Hats off to supputuri