繁体   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