簡體   English   中英

如何使用Selenium Webdriver查找網頁上多個按鈕的數量

[英]How to find the count of multiple buttons on a webpage using Selenium Webdriver

我在網頁上有4個上傳按鈕。 每個上傳按鈕都具有上傳文件的通用功能。

我無法使用Selenium webdriver獲取這些按鈕的數量。 按鈕的ID是:

  • buttonUpload_1
  • buttonUpload_2
  • buttonUpload_3
  • buttonUpload_4。

這些按鈕的通用實體是類名buttonSecondary smallButton

我已經嘗試了以下命令來獲取計數,但無法:

List<WebElement> buttoncount = driver.findElements(By.className(("buttonSecondary smallButton")));

List<WebElement> buttoncount = driver.findElements(By.xpath("//input[@class='buttonSecondary smallButton']"));

您也可以使用標記名進行計數

List<WebElement> buttons = driver.findElements(By.tagName("button"));
int buttonCount=0;
for(WebElement a : buttons){        
    if(a.getText().equals("buttonName")){
          buttonCount++;
}   
    System.out.println(buttonCount);
}

您可以使用By.xpath定位器, starts-with()函數和獲取size()來解決它:

List<WebElement> buttons = driver.findElements(By.xpath("//button[starts-with(@id, 'buttonUpload_')]"));
System.out.println(buttons.size());

如果你的所有按鈕都有相同的類或xpath,就像你有問題,那么你可以得到如下所示的總按鈕數:

 System.out.Println(buttoncount.size());

Size()將返回總數。 按鈕。

暫無
暫無

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

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