2015-05-06 28 views
1

我在网页上有4个Upload按钮。每个上传按钮都具有上传文件的通用功能。如何使用Selenium Webdriver查找网页上多个按钮的数量

我无法使用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']")); 

回答

1

你可以用By.xpath解决它定位器,starts-with()功能和获取size()

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

感谢您的建议。但是,我将计数作为零,而不是正确的计数。 你的建议确实有意义,但不幸的是它不工作.. 任何其他建议使用其名称的一部分来标识元素? –

+0

@BAshwin考虑到你在问题中提供了什么,它应该起作用。提供元素的HTML表示或链接到我们可以看到它的网站。另外,页面上是否有任何iframe?谢谢。 – alecxe

+0

Here you go,下面是html代码:

0

如果您所有的按钮都会有相同的类或XPath就像你已经采取了问题,那么你可以得到总按钮数,如:

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

尺寸()将返回你总NUM。的按钮。

0

你可以指望使用标记名以及

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); 
} 
相关问题