Munni
Munni

Reputation: 77

selenium dynamically click li item

I am trying to dynamically search an "li" tag item and double-click on this website: www.jstree.com (the right-top hierarchy tree sample). The code does find the WebElement but does not do anything. I am trying as follows. Can someone please point to what I am doing wrong? I am using Firefox 35.0.1 and selenium 2.44.0.

    driver.get(baseUrl + "http://www.jstree.com/");
    WebElement we = driver.findElement(By.xpath("/html/body/div/div/div[1]/div[1]/div[2]/div[1]/ul/li[1]/ul"));
    Actions action = new Actions(driver);

    List<WebElement> liItems = we.findElements(By.tagName("li"));
    for(WebElement liItem:liItems)
    {
        System.out.println(liItem.getText());
        if(liItem.getText().startsWith("initially open"))
        {           
            System.out.println("Found it...");
            liItem.click();
            action.moveToElement(liItem).doubleClick().build().perform();
            break;
        }
    }

Upvotes: 2

Views: 2816

Answers (1)

Saifur
Saifur

Reputation: 16201

I ended up doing this:

Modified the selector to make sure ONLY the expected elements are returned. It helps a lot in terms of execution time and reducing the number of unwanted loopings. And, then find the element in run time and use Action() on that to perform double click. I also update the Selenium binding as @alecxe suggested to work with latest Firefox

public void DemoTest() throws InterruptedException {

        List<WebElement> liItems = driver.findElements(By.xpath("//*[contains(text(),'initially open')]"));

        for(WebElement liItem:liItems)
        {
            Actions actions = new Actions(driver);
            actions.moveToElement(liItem).doubleClick().build().perform();
        }
    }

Upvotes: 3

Related Questions