0

HTML Code:

<div id="ajax_listOfOptions" style="display: block; top: 243px; left: 373px; z-index: 10009;">
    <div id="13020007" class="optionDivSelected">Infosys Ltd.</div>
    <div id="14094980" class="optionDiv">India Infrastructure Trust</div>
    <div id="13020033" class="optionDiv">Tata Consultancy Services Ltd.</div>
    <div id="12150008" class="optionDiv">Reliance Industries Ltd.</div>
    <div id="14030001" class="optionDiv">State Bank of India</div>
    <div id="14030055" class="optionDiv">HDFC Bank Ltd.</div>
    <div id="12520002" class="optionDiv">Hindustan Unilever Ltd.</div>
    <div id="14080001" class="optionDiv">Housing Development Finance Corporation Ltd.</div>
    <div id="14030056" class="optionDiv">ICICI Bank Ltd.</div>
    <div id="14060023" class="optionDiv">Bajaj Finance Ltd.</div>
</div>

Sample Code:

List<WebElement>li=driver.findElements(By.id("ajax_listOfOptions"));
for(WebElement wb:li)
{
if(wb.getText().contains("Tata"))
System.out.print(wb.getText());
}

This should only print the string that contains "Tata"(i.e. Tata Consultancy Services), but the code is printing all the string in the list

Note: The list contains strings populated with ajax auto suggestions.

Output:

Infosys Ltd.
India Infrastructure Trust
Tata Consultancy Services Ltd.
Reliance Industries Ltd.
State Bank of India
HDFC Bank Ltd.
Hindustan Unilever Ltd.
Housing Development Finance Corporation Ltd.
ICICI Bank Ltd.
Bajaj Finance Ltd.
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • You need to loop over the child elements of the only item in the list returned by `driver.findElements(By.id("ajax_listOfOptions"));`. – f1sh Dec 05 '21 at 16:59
  • When i am using Listli=driver.findElements(By.className("optionDiv")); then it is working fine, but not with above code. Why? – Shashwat Aryan Dec 05 '21 at 17:12
  • because your code above finds the `div id="ajax_listOfOptions"` and performs a getText on it – f1sh Dec 05 '21 at 17:15
  • If you use `By.className("optionDiv")`, _Infosys Ltd._ which contains _`optionDivSelected`_ will never be selected within the list. See my [answer](https://stackoverflow.com/a/70236703/7429447) below. – undetected Selenium Dec 05 '21 at 17:28
  • Yes got it. Thanks a lot – Shashwat Aryan Dec 06 '21 at 17:17

1 Answers1

0

As per your line of code you are using By.id("ajax_listOfOptions") which identifies the entire TABLE element. So the list LI contains only one element which does includes the string Tata. Hence the entire TABLE text is printed.


To print the text Tata Consultancy Services Ltd. you can use the following Locator Strategies:

  • Using cssSelector:

    List<WebElement>li=driver.findElements(By.cssSelector("div#ajax_listOfOptions div"));
    for(WebElement wb:li)
    {
        if(wb.getText().contains("Tata"))
            System.out.print(wb.getText());
    }
    
  • Using xpath:

    List<WebElement>li=driver.findElements(By.xpath("//div[@id='ajax_listOfOptions']//div"));
    for(WebElement wb:li)
    {
        if(wb.getText().contains("Tata"))
            System.out.print(wb.getText());
    }
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352