简体   繁体   English

如何使用Selenium Webdriver查找网页上多个按钮的数量

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

I have 4 Upload buttons on a webpage. 我在网页上有4个上传按钮。 Each of the upload button has the common functionality of uploading a file. 每个上传按钮都具有上传文件的通用功能。

I am unable to get the count of these buttons using Selenium webdriver. 我无法使用Selenium webdriver获取这些按钮的数量。 The id of the buttons are: 按钮的ID是:

  • buttonUpload_1 buttonUpload_1
  • buttonUpload_2 buttonUpload_2
  • buttonUpload_3 buttonUpload_3
  • buttonUpload_4. buttonUpload_4。

The common entity for these buttons is the class name buttonSecondary smallButton 这些按钮的通用实体是类名buttonSecondary smallButton

I have tried the below commands to get the count, but was unable to: 我已经尝试了以下命令来获取计数,但无法:

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

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

You can get count using tagname as well 您也可以使用标记名进行计数

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);
}

You can solve it with By.xpath locator, starts-with() function and getting the size() : 您可以使用By.xpath定位器, starts-with()函数和获取size()来解决它:

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

If your all button will have same class OR xpath like you have taken in question then you can get total button count like : 如果你的所有按钮都有相同的类或xpath,就像你有问题,那么你可以得到如下所示的总按钮数:

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

Size() will return you total num. Size()将返回总数。 of buttons. 按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Selenium Webdriver在新网页中查找元素? - How to find an element in new Webpage using selenium webdriver? 如何使用 Selenium Webdriver 在网页中找到“光标”位置? - How to find “Cursor” position in webpage with Selenium Webdriver? 如何使用Selenium WebDriver配置网页语言 - How to configure webpage language using selenium webdriver 如何使用Selenium Webdriver正确查找网页的元素? - How does correctly find elements of a webpage with Selenium Webdriver? 使用 Selenium WebDriver 在 instagram 中查找并单击所有“关注”按钮 - Find and click on all “follow” buttons in instagram using Selenium WebDriver 如何在网页上使用带有 Java 的 Selenium WebDriver 验证工具提示文本 - How to verify tooltip text using Selenium WebDriver with java on a webpage 如何使用Selenium WebDriver捕获网页的屏幕截图? - How to capture the screen shot of a webpage using selenium webdriver? 如何使用Selenium WebDriver选择网页上的两个元素之一? - How to chose either of 2 elements on a Webpage using Selenium WebDriver? 如何使用Selenium WebDriver移至网页中的其他选项卡? - How to move to different tabs in a webpage using selenium webdriver? 如何使用硒webdriver查找是否启用了元素? - How to find an element is enabled or not using selenium webdriver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM