Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading