【发布时间】:2019-12-05 20:07:49
【问题描述】:
我正在尝试按给定类获取 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>
要按类从元素中获取文本,可以这样做:
driver.findElementByClass("button").Attribute("innerText")
输出:蓝莓
现在,使用Xpath获取某个类的某一项,如下:
driver.findElementsByXpath("//*[contains(@id,'pancakes')]/button").item[1].Attribute("innerText")
输出:草莓
我需要的是获得“按钮”的总数。所以我需要一个 输出 来给我“4”(因为有 4 个“按钮”)
我还没有找到在 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
【问题讨论】:
-
试试
driver.findElementsByXpath("//*[contains(@id,'pancakes')]/button").Count()。 -
DUUUUUUUUUUUUUUUUUUUUUUUUUUUUUDE 它有效!谢谢!经过数小时的搜索,是否就这么简单,我认为无法在这里设置您的答案:/
-
还有一件事,你知道我在哪里可以找到更多我可以使用的功能/命令(比如“.Count”)吗?
标签: javascript selenium autohotkey