在Java-Selenium中查找网页上文本框的总数

zaq34kh6  于 2022-12-18  发布在  Java
关注(0)|答案(3)|浏览(123)

I am looking for some help as am just getting into a web automation using Selenium with the Java bindings. I am trying to find the total number of text boxes on a web page, I have the code working for checking @type=text . The code is below, however, when I try to find text boxes by:
"//input[@type='text'[@class='dijitReset dijitInputInner']" and it fails. The full code is below.

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Launch browser
driver.get("http://localhost/2010A15/?p=register");

//Create Web element list

java.util.List<WebElement> textboxes = driver.findElements(By.xpath("dijitReset dijitInputInner']"));

System.out.println(textboxes.size());

for(int i=1; i<=textboxes.size(); i=i+1);
{
    System.out.println(textboxes.size());
}

driver.close();  



}

}
Error Message:
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: The given selector dijitReset dijitInputInner'] is either invalid or does not result in a WebElement. The following error occurred: InvalidSelectorError: Unable to locate an element with the xpath expression dijitReset dijitInputInner'] because of the following error: SyntaxError: The expression is not a legal expression.
I presume the error is telling me that dijitReset dijitInputInner is not valid and that's why it isn't working, however, I am unsure on how to fix this. Any help? As the class of the item in inspect element is: dijitReset dijitInputInner .

crcmnpdw

crcmnpdw1#

如果你只想计算元素(文本框)的数量,那么你可以通过使用size()来避免循环每个webelement

int boxes = driver.findElements(By.xpath(".//*[@class='dijitReset dijitInputInner']")).size();
kb5ga3dv

kb5ga3dv2#

List<WebElements> totalTextboxes = driver.findElements(By.xpath("//input[@type='text']"));
System.out.println(totalTextboxes.size());
dgenwo3n

dgenwo3n3#

List<WebElement> textBoxes=driver.findElements(By.xpath(//input[@type='text']));
i=textBoxes.size(); 

List<WebElement> textBoxes=driver.findElements(By.xpath(//input));
for(WebElement each:textBoxes){

   String local=each.getAttribute("type");
    if(local!=null && local.equals("text") i++;
}

相关问题