[英]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是:
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.