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

Time:07-11

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 : 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  style="display: block;">
<div >
    <div  data-index="0"><span >elephant toothpaste</span></div>
    <div  data-index="1"><span >elephant toothpaste</span> experiment</div>
    ...
</div>
<div >
    <span >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 ?

CodePudding user response:

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.

  • Related