簡體   English   中英

如何使用Selenium WebDriver和Autohotkey獲取HTML中的元素總數?

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

我正在嘗試通過給定的類獲取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個“ buttons”

我還沒有辦法在Autohotkey中做到這一點。 我看過其他語言的解決方案,例如

一種

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

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

我希望以任何可能的方式獲得 項目總數

我正在考慮尚未建立的Selenium的某些功能,但我不知道(例如某些-在行的末尾-“ .len”,“。size”,“。count”,但它們都不起作用為了我)

任何建議都值得歡迎和贊賞,謝謝!

編輯:哇,我只是在“ .Count ”上缺少“ ()

這就是我想要的

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

感謝supputuri

您可以使用Count()方法獲取與xpath匹配的元素數。

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

您可以參考此以獲取更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM