简体   繁体   English

遍历具有相同属性的输入对象的集合:Java Selenium Webdriver

[英]Iterate over collection of input objects with identical attributes: Java Selenium Webdriver

I have tried some of the examples of iteration already provided here, but I am not yet able to iterate over a collection of input fields, all of which have identical identification attributes: , and . 我已经尝试了此处已经提供的一些迭代示例,但是我还不能迭代输入字段的集合,所有输入字段都具有相同的标识属性:和。

I need to iterate over all these objects and add the same string to each. 我需要遍历所有这些对象,并向每个对象添加相同的字符串。 The following code assigns the string to the first instance of the input field, but it will not move on to the next instance: 以下代码将字符串分配给输入字段的第一个实例,但不会继续进行下一个实例:

List<WebElement> allSumInsuredFields = driver.findElements(By.id("ctl00__txtSumInsured"));
Iterator<WebElement> itr = allSumInsuredFields.iterator();
while(itr.hasNext()){

    if (driver.findElement(By.id("ctl00_txtSumInsured")).getText().equals("")) ;
        driver.findElement(By.id("ctl00_txtSumInsured")).sendKeys("£250000");

    itr.next(); 
}

The main issue with your code is that inside your loop you use driver.findElement to acquire the element to work on. 代码的主要问题是在循环内部,您使用driver.findElement获取要处理的元素。 Not only is this doing extra work but this makes your algorithm incorrect . 这不仅会做额外的工作还会使您的算法不正确 The findElement methods (in the singular) return the first element they find. findElement方法(单数形式)返回找到的第一个元素 So in your code, it does not matter how many times the loop iterates, it is always going to operate on the first element with the id you want. 因此,在您的代码中,循环迭代多少次并不重要,它始终将在具有所需ID的第一个元素上进行操作。

As Saifur mentioned, you should be using getAttribute("value") to get the value of an input element. 正如Saifur所述,您应该使用getAttribute("value")来获取input元素的值。 So something like this is what you need: 因此,您需要这样的东西:

for(WebElement el : driver.findElements(By.id("ctl00__txtSumInsured"))) {
    if (el.getAttribute("value").equals(""))        
        el.sendKeys("something");
}

Contrarily to Saifur, I'm not suggesting that you use an explicit wait before this loop. 与Saifur相反,我不建议您在此循环之前使用显式等待。 Whether or not you need one, and what form it should take really depends on the application being tested. 是否需要一个,以及它应该采用哪种形式,实际上取决于要测试的应用程序。 You did not say enough in the question to make this determination. 您在问题中的发言不足以做出此决定。 An extra, unnecessary check is not harmless: it makes your Selenium script slower. 额外的不必要检查并非无害:它会使您的Selenium脚本变慢。 If you get into the habit of having unnecessary checks you can add minutes of running time to your script. 如果您习惯于进行不必要的检查,则可以在脚本中添加几分钟的运行时间。 And a check that does not check for the exact right thing is actually harmful. 而没有检查确切正确的事情的检查实际上是有害的。

Does not need to use extra iterator 不需要使用额外的迭代器

By byId = By.id("ctl00__txtSumInsured");

//Explicit wait just to make sure the elements are loaded properly
(new WebDriverWait(driver, 10))
  .until(ExpectedConditions.presenceOfElementLocated(byId));

List<WebElement> allSumInsuredFields = driver.findElements(byId);

//Now iterate through List and do whatever you want
for(WebElement ele:allSumInsuredFields)
{
  if(ele.getAttribute("value").equals(""));        
  ele.sendKeys("something");

}

EDIT You don't use getText() to get the value of input textbox You should replace that with getAttribute("value") and that will return the value 编辑您不要使用getText()来获取输入文本框的值,应将其替换为getAttribute("value") ,这将返回值

if (driver.findElement(By.id("ctl00_txtSumInsured")).getAttribute("value").equals("")) ;

I would approach it like this: 我会这样处理:

C# C#

List<IWebElement> allSumInsuredFields = driver.findElements(By.Id("ctl00__txtSumInsured"));

foreach (var element in allSumInsuredFields)
   {
       if (string.IsNullOrEmpty(element.Text))
       {
           element.SendKeys("£250000");
       }
    }

Java: Java的:

for(WebElement element : allSumInsuredFields)     
           if (element.getAttribute("value") != null && 
              !element.getAttribute("value").isEmpty())
           {
               element.sendKeys("£250000");
           }

Apologies if the Java syntax is incorrect, but you get what I'm aiming at here. 如果Java语法不正确,我们深表歉意,但是您明白我的目标了。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM