Java Selenium webdriver access span element inside each div element from a autosuggest

Issue

am a newbie to selenium and am struggling with what looks to be a simple ask.
Using Java 8 with selenium webdriver and Chrome.

My requirement is to go to this website : https://start.duckduckgo.com/

In the search type : elephant
enter image description here

Now I need to retrieve all the autosuggested values using selenium.
From browser console I think this is the necessary HTML containing the auto suggestions :

<div class="search__autocomplete" style="display: block;">
<div class="acp-wrap js-acp-wrap">
    <div class="acp" data-index="0"><span class="t-normal">elephant toothpaste</span></div>
    <div class="acp" data-index="1"><span class="t-normal">elephant toothpaste</span> experiment</div>
    ...
</div>
<div class="acp-footer is-hidden js-acp-footer">
    <span class="acp-footer__instructions">Shortcuts to other sites to search off DuckDuckGo</span>
</div>

Here is the code I have tried with no luck :

WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://start.duckduckgo.com/");
WebElement searchText = driver.findElement(By.name("q"));
searchText.sendKeys("elephant");

List<WebElement> searchList = new ArrayList<WebElement>();


// have tried various options : all of the below selectors return no results

//searchList = driver.findElements(By.xpath("//div[contains(@class, 'acp')]//span"));
//searchList = driver.findElements(By.xpath("//div[contains(@class, 
      'acp')]//span[contains(@class, 't-normal')]"));
//searchList = driver.findElements(By.cssSelector(".t-normal"));
//searchList = driver.findElements(By.cssSelector("t-normal"));
//searchList = driver.findElements(By.cssSelector(".acp > span"));

// this atleast returns a collection of 10 but not sure of its content
searchList = driver.findElements(By.xpath("//div[contains(@class, 'acp')]"));

if(null != searchList && searchList.size() > 0) {
        for(int i = 0;i<searchList.size();i++) {
            WebElement e = searchList.get(i);
            
            System.out.println("element details are " + e.toString());
            /** NoSuchElementException with below  tried Xpath and by css 
            WebElement spanElement = e.findElement(By.className("t-normal"));
            WebElement spanElement = e.findElement(By.className(".t-normal"));
            WebElement spanElement = e.findElement(By.cssSelector(".t-normal"));
            WebElement spanElement = e.findElement(By.cssSelector("t-normal"));
            **/
            
            WebElement spanElement = e.findElement(By.cssSelector("t-normal"));
    
            System.out.println(e.getText());
            
            
        }
    }else {
        System.out.println("<<<<< not able to locate >>>>>");
    }

If in browser I do a find using xpath I can locate these span elements :
//div[contains(@class, 'acp-wrap js-acp-wrap')]//div[contains(@class, 'acp')]//span[contains(@class,'t-normal')]

So really confused on how to extract text from within span ?

Solution

searchList = driver.findElements(By.xpath("//div[@class='acp']"))
for(int i = 0;i<searchList.size();i++) {
    System.out.println(searchList.get(i).getText());

Just get all 8 dropdown values and then print it’s getText.

Answered By – Arundeep Chohan

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published