Search code examples
javaseleniumarraylistselenium-webdriverfirebug

Selenium-Java-FireBug : FirePath returns 9 matching nodes where as List<WebElement> is returning 18 elements


FirePath is returning 9 matching nodes where as List is returning 18 elements.

OS: Win8 Pro, 64 bit

Java: jdk1.8.0_77

Selenium: 3.4.0 (selenium-server-standalone)

GeckoDriver: 0.17.0

Browser: Mozilla Firefox 53.0

IDE: Eclipse Neon.2 Release (4.6.2)

FireBug: 2.0.18

URL: https://demos.devexpress.com/aspxeditorsdemos/ListEditors/MultiSelect.aspx

XPATH: //table[@id='ContentHolder_lbFeatures_LBT']/tbody/tr/td

I am trying to get the number of items from a List Box with Multiple Selection. When I provide the xpath in FireBug/FirePath, it returns me "9 matching nodes".

enter image description here

Moving forward, through my script, I add the WebElements in a generic List of type WebElement through findElements method. Next when I call size() method for the List<WebElement>, it returns me 18 Elements

Update:

(Apologies, I made a mistake in putting up the Question with exact steps while trying to narrow down to the exact problem)

Here is the complete issue.

Steps Required:

  1. Access the URL.
  2. Click on Selection mode as Multiple
  3. From Phone features table, I need to select Blue Tooth, Memory Card Slot and Touch screen. The List may vary, so I want to keep it in a List<String>.

Here is my script:

package demo;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Q45065876_keyDown {

    public static void main(String[] args) {


        System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.navigate().to("https://demos.devexpress.com/aspxeditorsdemos/ListEditors/MultiSelect.aspx");

        driver.findElement(By.xpath("//input[@id='ContentHolder_lbSelectionMode_I']")).click();
        List<WebElement> selection_list = driver.findElements(By.xpath("//table[@id='ContentHolder_lbSelectionMode_DDD_L_LBT']/tbody/tr/td"));
        for (WebElement ele:selection_list)
        {
        if(ele.getAttribute("innerHTML").contentEquals("Multiple"))
            ele.click();
            break;
        }
        driver.findElement(By.id("ContentHolder_lbSelectionMode_DDD_L_LBI1T0")).click();
        List<WebElement> phone_feature_list = driver.findElements(By.xpath("//table[@id='ContentHolder_lbFeatures_LBT']/tbody/tr/td"));
        System.out.println("Number of Elements : "+phone_feature_list.size());
        List<String> item_list = new ArrayList<String>();
        item_list.add("Bluetooth");
        item_list.add("Memory Card Slot");
        item_list.add("Touch screen");
        System.out.println("Number of Elements : "+item_list.size());

        for (int i=0; i<phone_feature_list.size(); i++)
        {
            WebElement my_element = phone_feature_list.get(i);
            String innerhtml = my_element.getAttribute("innerHTML");
             System.out.println("INNER HTML : "+innerhtml);
            for (int j=0; j<item_list.size(); j++)
            {
                item_list.get(j).contentEquals(innerhtml);
                my_element.click();
            }
        }
    }
}

Can you please help me out to understand whats wrong happening here? Thank you all for all the help.


Solution

  • It take a second or two for the site to refresh. The xpath

    "//table[@id='ContentHolder_lbFeatures_LBT']/tbody/tr/td"
    

    includes those checkboxes so you are getting 18 results. You can wait until there is different number of results

    List<WebElement> phone_feature_list = driver.findElements(By.xpath("//table[@id='ContentHolder_lbFeatures_LBT']/tbody/tr/td[contains(@class, 'dxeT')]"));
    int size = phone_feature_list.size();
    
    // choose an option from the dropdown
    
    // wait for the size to change
    while ((phone_feature_list = driver.findElements(By.xpath("//table[@id='ContentHolder_lbFeatures_LBT']/tbody/tr/td[contains(@class, 'dxeT')]"))).size() == size);
    
    System.out.println("Number of Elements : " + phone_feature_list.size());