Java 如何使用SeleniumWebDriver查找网页上多个按钮的计数
我在一个网页上有4个上传按钮。每个上载按钮都具有上载文件的通用功能 我无法使用SeleniumWebDriver获得这些按钮的数量。 按钮的id为:Java 如何使用SeleniumWebDriver查找网页上多个按钮的计数,java,selenium,xpath,selenium-webdriver,Java,Selenium,Xpath,Selenium Webdriver,我在一个网页上有4个上传按钮。每个上载按钮都具有上载文件的通用功能 我无法使用SeleniumWebDriver获得这些按钮的数量。 按钮的id为: 按钮按钮按钮1 按钮按钮按钮2 按钮按钮按钮3 按钮打开4 这些按钮的通用实体是类名buttonSecondary smallButton 我已尝试使用以下命令获取计数,但无法: List<WebElement> buttoncount = driver.findElements(By.className(("buttonSecon
- 按钮按钮按钮1
- 按钮按钮按钮2
- 按钮按钮按钮3
- 按钮打开4李>
buttonSecondary smallButton
我已尝试使用以下命令获取计数,但无法:
List<WebElement> buttoncount = driver.findElements(By.className(("buttonSecondary smallButton")));
List<WebElement> buttoncount = driver.findElements(By.xpath("//input[@class='buttonSecondary smallButton']"));
List buttoncount=driver.findElements(By.className((“buttonSecondary smallButton”));
List buttoncount=driver.findElements(By.xpath(//input[@class='buttonSecondary smallButton']);
您可以通过.xpath使用定位器来解决它,启动-with()
函数并获取大小()
:
List buttons=driver.findElements(By.xpath(//button[以(@id,'buttonUpload'))开头);
System.out.println(buttons.size());
如果您的“全部”按钮将具有与您所讨论的相同的类或xpath,那么您可以获得总按钮数,如下所示:
System.out.Println(buttoncount.size());
Size()将返回按钮总数。您也可以使用标记名获取计数
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);
}
List buttons=driver.findElements(按.tagName(“按钮”);
int buttonCount=0;
对于(WebElement a:按钮){
如果(a.getText().equals(“buttonName”)){
按钮计数++;
}
系统输出打印项次(按钮计数);
}
谢谢你的建议。但是,我得到的计数为零,而不是正确的计数。你的建议是有道理的,但不幸的是它不起作用。。关于使用元素名称的一部分来标识元素的任何其他建议?@BAshwin考虑到您在问题中提供的内容,它应该会起作用。提供元素的HTML表示形式,或者提供指向我们可以看到它的网站的链接。另外,页面上是否有iFrame?谢谢。给你,下面是html表示:@BAshwin,在这里,这些是按钮
元素,不是输入
,修复了。ok。那么,不可能计算固定按钮元素的数量吗?谢谢
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);
}