0

I have a Google Search bar with a drop down and the options are under a UL and the actual ones under the li. I am unable to retrieve the data field of the li, while I am able to retrieve the count. Have tried with the List elements and looping thru it ,but no luck. The drop with all the Options is Not visible. I have to print the Titles associated with the li using Selenium Web driver.

Below is what I tried:

ul class=classname> xxx – GSA 2 days ago

wb.findElement(By.xpath("<>")).click(); List items = html_list.findElements(By.tagName("li")); System.out.println("The size of the elements is " + items.size()); for(WebElement item : items) { html_list.sendKeys( Keys.DOWN ); //simulate visual movement wb.manage().timeouts().implicitlyWait(1000, TimeUnit.SECONDS); System.out.println(item.getText()); }

5
  • Post your code and html. Show us what have you tried so far.
    – Fenio
    Dec 16, 2016 at 12:56
  • As Rafal said, we need to see some code to help you. Dec 16, 2016 at 13:07
  • ul class=classname> <li title="xxx" data-value="xxx">xxx</li> </ul>
    – GSA
    Dec 16, 2016 at 13:37
  • wb.findElement(By.xpath("<<xpath>>")).click(); List<WebElement> items = html_list.findElements(By.tagName("li")); System.out.println("The size of the elements is " + items.size()); for(WebElement item : items) { html_list.sendKeys( Keys.DOWN ); //simulate visual movement wb.manage().timeouts().implicitlyWait(1000, TimeUnit.SECONDS); System.out.println(item.getText()); }
    – GSA
    Dec 16, 2016 at 13:45
  • 1
    Please edit your question and add all the relevant data rather than posting it all in comments. It will help later readers more easily find it.
    – JeffC
    Dec 16, 2016 at 20:41

2 Answers 2

0

Hypohesis #1: You don't wait for the page to load after the click on line 1. The li elements are in the DOM but they are not completely loaded. You could test the hypothesis by introducing a delay between line 1 and line 2. I have code that I use to wait for page loads but the specifics depend on the JavaScript framework (e.g. jQuery, AngularJs), on the loading spinner implementation and browser readystate. My readystate logic looks like this:

/**
 * Wait for browser readystate to be complete. But don't wait forever.
 * 
 * @param webDriver
 */
public void waitForBrowserReadystateComplete(WebDriver webDriver) {
    for (int a=0; a<20; a++) {
        JavascriptExecutor javascriptExecutor = (JavascriptExecutor) webDriver;
        if (javascriptExecutor.executeScript("return document.readyState")
                .toString().equals("complete")) {
            break;
        }
        sleepResponsibly(500);
    }
}

public void sleepResponsibly(int timeMillisecond){
    try{
        Thread.sleep(timeMillisecond);
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt(); 
        throw new RuntimeException(ex);
    }
}

Hypthosis #2: The text nodes are complex. I have experienced a failure in method getText() to return text when the text nodes code complex formatting. I got around this by invoking the following instead:

public static String getTextFromElementsTextNodes(WebDriver webDriver, WebElement element) throws IllegalArgumentException {

    String text = "";
    if (webDriver instanceof JavascriptExecutor) {
        text = (String)((JavascriptExecutor) webDriver).executeScript(
                "var nodes = arguments[0].childNodes;" +
                "var text = '';" +
                "for (var i = 0; i < nodes.length; i++) {" +
                "    if (nodes[i].nodeType == Node.TEXT_NODE) {" +
                "        text += nodes[i].textContent;" +
                "    }" +
                "}" +
                "return text;"
                , element);
    } else {
        throw new IllegalArgumentException("driver is not an instance of JavascriptExecutor");
    }
    return text;
}

A caveat is that getTextFromElementsTextNodes() may return non-ASCII charcters so I do the following:

System.out.println(getTextFromElementsTextNodes(wb,item).replaceAll("[^\\x00-\\x7F]", " "));
2
  • Hi Mike,I get error when I try to execute the code you have provided.
    – GSA
    Dec 20, 2016 at 10:46
  • What error? Please post the complete message and the code that generated it. You can edit your question and add it there. Dec 20, 2016 at 13:24
0

I could finally able to retrieve the options under the li. This is what I did and it worked for(WebElement MyListOptions: listOfLiTags) {

           String myoptions=serchOptions.getAttribute("<<The actual attribute value which is storing my options>>");

          System.out.println("THE List Options are" + txt3);
      }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.