0

I have to automate the functionality of search.There are 2 possible out comes. one is result is generated the other is results not found. if there are matching results to search criteria a new div element is visible with results. If there are no results results not found message will display in a separate div element. Only one div element is visible at a time. so I have verified the search results as below.

Boolean resultPresent = driver
            .findElement(By.xpath("//div[@data-once='ajax-pager']/div[@class='view-content row']")).isDisplayed();

    if (resultPresent == true) {
        Assert.assertTrue(resultPresent);
        
        Assert.assertTrue(driver.findElement(By.xpath("//div[@class='product-taeser-view']")).isDisplayed());
    } else {

        System.out.print("no products");

        List<WebElement> emptyMsg = driver.findElements(By.xpath("//div[@class='view-empty']/p"));

        Assert.assertTrue(emptyMsg.get(0).isDisplayed());
        Assert.assertEquals(emptyMsg.get(0).getText(), "Couldn’t find any devices");

        Assert.assertTrue(emptyMsg.get(1).isDisplayed());
        Assert.assertEquals(emptyMsg.get(1).getText(),
                "It seems we don’t have any devices with the filters you have selected at the moment. Try using a different set of filters");
    }

What I have done is, check the visibility of search results and used that to validate results or the results not found value.

But when there are no results to I'm getting org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@data-once='ajax-pager']/div[@class='view-content row']"}

Below is the html code for results

    <div class="views-element-container container-fluid"><div class="product-catalog-view view view-phone-catalog view-id-phone_catalog view-display-id-block_phone_catalog js-view-dom-id-285cae00df842a0b6be483f000119c9f43711985a6974010b9f451e16dd8c6f5" data-once="ajax-pager">
  
    
        <div class="view-filters">
      
<form class="views-exposed-form" data-drupal-selector="views-exposed-form-phone-catalog-block-phone-catalog" action="/shop/phones" method="get" id="views-exposed-form-phone-catalog-block-phone-catalog" accept-charset="UTF-8" data-once="exposed-form">
    <details class="filter-block js-form-wrapper form-wrapper" data-drupal-selector="edit-filter-block" aria-describedby="edit-filter-block--description" id="edit-filter-block" open="open">
  <summary role="button" aria-controls="edit-filter-block" aria-expanded="true" aria-pressed="true" style="display: none;">FILTER<span class="show"><i class="fas fa-chevron-down"></i></span>
    <span class="hide"><i class="fas fa-chevron-up"></i></span>
    <span class="summary"></span>
  </summary>
  <div class="form-row">
    



  <fieldset class="js-form-item js-form-type-cshs form-type-cshs js-form-item-brand form-item-brand mb-3">
          <label for="edit-brand--3gXefRvFb2g">Brand/Family</label>
                    <div class="shop-cshs-field">
  <select data-drupal-selector="edit-brand" class="simpler-select-root form-element form-element--type-select form-select" id="edit-brand--3gXefRvFb2g" name="brand" style="display: none;">
                  <option value="All" data-parent="" selected="selected">
          All
        </option>
                        <option value="1001" data-parent="">
          Apple
        </option>
                        <option value="1005" data-parent="1001">
          iPhone 10
        </option>
                        <option value="1004" data-parent="1001">
          iPhone 11
        </option>
                        <option value="1003" data-parent="1001">
          iPhone 12
        </option>
                        <option value="1002" data-parent="1001">
          iPhone 13
        </option>
                        <option value="1192" data-parent="1001">
          iPhone 14
        </option>
                        <option value="1134" data-parent="">
          Phone ABC
        </option>
                        <option value="1193" data-parent="">
          Phone DEF
        </option>
                        <option value="1194" data-parent="1193">
          A
        </option>
                        <option value="997" data-parent="">
          Samsung
        </option>
                        <option value="998" data-parent="997">
          A Series
        </option>
                        <option value="1000" data-parent="997">
          Note Series
        </option>
                        <option value="999" data-parent="997">
          S Series
        </option>
                        <option value="1006" data-parent="">
          Huawei
        </option>
                        <option value="1007" data-parent="">
          Oppo
        </option>
                        <option value="1008" data-parent="1007">
          Series 60
        </option>
            </select><div class="select-wrapper select-wrapper--level-0" data-level="0"><select class="simpler-select form-element form-element--type-select form-select" id="edit-brand--3gXefRvFb2g--level-0"><option value="All">All</option><option value="1001" class="has-children">Apple</option><option value="1134">Phone ABC</option><option value="1193" class="has-children">Phone DEF</option><option value="997" class="has-children">Samsung</option><option value="1006">Huawei</option><option value="1007" class="has-children">Oppo</option></select></div>
</div>
                      </fieldset>




  <fieldset class="js-form-item js-form-type-select form-type-select js-form-item-price form-item-price mb-3">
          <label for="edit-price--fTIWV5xHUuQ">Price range</label>
                    
<select data-drupal-selector="edit-price" id="edit-price--fTIWV5xHUuQ" name="price" class="form-select"><option value="All" selected="selected">All</option><option value="1">Below Rs. 10,000</option><option value="2">Rs. 10,000 - 25,000</option><option value="3">Rs. 25,000 - 50,000</option><option value="4">Rs. 50,000 - 100,000</option><option value="5">Rs. 100,000 - 200,000</option><option value="6">Above Rs. 200,000</option></select>
                      </fieldset>




  <fieldset class="js-form-item js-form-type-select form-type-select js-form-item-cameras form-item-cameras mb-3">
          <label for="edit-cameras--hPq_KbJeRps">Number of Cameras</label>
                    
<select data-drupal-selector="edit-cameras" id="edit-cameras--hPq_KbJeRps" name="cameras" class="form-select"><option value="All">All</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4" selected="selected">More than 3</option></select>
                      </fieldset>
<div data-drupal-selector="edit-actions" class="form-actions js-form-wrapper form-group container-fluid" id="edit-actions--RZeuRpm2i6s"><button class="btn btn-danger button js-form-submit form-submit" data-drupal-selector="edit-submit-phone-catalog-1v9fjqnbt6a" type="submit" id="edit-submit-phone-catalog--1V9FjqnbT6A" value="FILTER">FILTER</button>
</div>

  </div>
</details>
<script>
  function setFilterVisibility() {
    if (window.matchMedia('(min-width: 768px)').matches) {
      // Hide the toggle button and display the filters
      $(".filter-block").attr('open', true);
      $(".filter-block > summary").hide();
    } else {
              $(".filter-block").attr('open', true);
        $(".filter-block > summary").show();
          }
  }

  setFilterVisibility();

  $(window).on('orientationchange resize', setFilterVisibility);
</script>
</form>

    </div>
    
      <div class="view-content row">
          <div class="product-teaser views-row">
    
<article class="commerce-product--phones--teaser commerce-product--teaser">
      <div class="no-promotions-block"></div>
    <div class="product-taeser-view">
    <div class="product-taeser-block1">
      
      <div class="product--variation-field--variation_field_images__5 field field--name-field-images field--type-image field--label-hidden field--items">
              <div class="field--item">  <img loading="lazy" src="https://dialog-qa-bucket.s3.ap-southeast-1.amazonaws.com/s3fs-public/styles/product_thumbnail/public/2022-03/Black%201.jpg?itok=hUs-qGRy" width="55" height="100" alt="Front view" typeof="foaf:Image" class="image-style-product-thumbnail">


</div>
          </div>
  
      <div class="product-taeser-block2">
        
            <div class="field field--name-title field--type-string field--label-hidden field--item">iPhone 12</div>
      
        <div class="price-label">Price</div>
        
            <div class="product--variation-field--variation_price__5 field field--name-price field--type-commerce-price field--label-hidden field--item">Rs. 3,000.00</div>
      
                  <span class="list-price"></span> <span class="discount-text">&nbsp;</span>
              </div>
    </div>
              
            <div class="clearfix text-formatted field field--name-field-mini-specification field--type-text-long field--label-hidden field--item"><ul>
    <li>6.1 inch Super Retina XDR display</li>
    <li>5G mmWave and sub-6GHz&nbsp;5G mmWave and sub-6GHz&nbsp;5G mmWave and sub-6GHz&nbsp;5G mmWave and sub-6GHz</li>
    <li>5G mmWave and sub-6GHz</li>
</ul>
</div>
      
        <div class="buy-now-button">
      <a href="/product/5" class="commerce-product--catalog__hover-text">BUY NOW</a>
    </div>
  </div>
</article>

  </div>
    <div class="product-teaser views-row">
    
<article class="commerce-product--phones--teaser commerce-product--teaser">
      <div class="promotions-block">
      <div class="promotions-text">
        
            <div class="field field--name-field-offer-tag-line field--type-string field--label-hidden field--item">10% OFF</div>
      
      </div>
    </div>
    <div class="product-taeser-view">
    <div class="product-taeser-block1">
      
      <div class="product--variation-field--variation_field_images__3 field field--name-field-images field--type-image field--label-hidden field--items">
              <div class="field--item">  <img loading="lazy" src="https://dialog-qa-bucket.s3.ap-southeast-1.amazonaws.com/s3fs-public/styles/product_thumbnail/public/2022-04/Black%201_0.jpg?itok=VtCJKZVP" width="81" height="100" alt="Front view" typeof="foaf:Image" class="image-style-product-thumbnail">


</div>
          </div>
  
      <div class="product-taeser-block2">
        
            <div class="field field--name-title field--type-string field--label-hidden field--item">Samsung Galaxy S21</div>
      
        <div class="price-label">Price</div>
        
            <div class="product--variation-field--variation_price__3 field field--name-price field--type-commerce-price field--label-hidden field--item">Rs. 92,500.00</div>
      
                  <span class="list-price"></span> <span class="discount-text">&nbsp;</span>
              </div>
    </div>
              
            <div class="clearfix text-formatted field field--name-field-mini-specification field--type-text-long field--label-hidden field--item"><ul>
    <li>256GB US Version Smartphone</li>
    <li>Pro-Grade Camera 8K Video 64MP High Res</li>
    <li>Phantom Black</li>
</ul>
</div>
      
        <div class="buy-now-button">
      <a href="/product/3" class="commerce-product--catalog__hover-text">BUY NOW</a>
    </div>
  </div>
</article>

  </div>
    <div class="product-teaser views-row">
    
<article class="commerce-product--phones--teaser commerce-product--teaser">
      <div class="promotions-block">
      <div class="promotions-text">
        
            <div class="field field--name-field-offer-tag-line field--type-string field--label-hidden field--item">20%  off</div>
      
      </div>
    </div>
    <div class="product-taeser-view">
    <div class="product-taeser-block1">
      
      <div class="product--variation-field--variation_field_images__4 field field--name-field-images field--type-image field--label-hidden field--items">
              <div class="field--item">  <img loading="lazy" src="https://dialog-qa-bucket.s3.ap-southeast-1.amazonaws.com/s3fs-public/styles/product_thumbnail/public/2022-04/Black%201.jpg?itok=8JDWTIiJ" width="81" height="100" alt="Front" typeof="foaf:Image" class="image-style-product-thumbnail">


</div>
          </div>
  
      <div class="product-taeser-block2">
        
            <div class="field field--name-title field--type-string field--label-hidden field--item">Samsung Galaxy S21 - V3</div>
      
        <div class="price-label">Price</div>
        
            <div class="product--variation-field--variation_price__4 field field--name-price field--type-commerce-price field--label-hidden field--item">Rs. 75,000.00</div>
      
                  <span class="list-price"></span> <span class="discount-text">&nbsp;</span>
              </div>
    </div>
              
            <div class="clearfix text-formatted field field--name-field-mini-specification field--type-text-long field--label-hidden field--item"><ul>
    <li>256GB US Version Smartphone</li>
    <li>Pro-Grade Camera 8K Video 64MP High Res</li>
    <li>Phantom Black</li>
</ul>
</div>
      
        <div class="buy-now-button">
      <a href="/product/4" class="commerce-product--catalog__hover-text">BUY NOW</a>
    </div>
  </div>
</article>

  </div>

    </div>
  
      
          </div>
</div>

Below is the html code for results not found

    <div class="views-element-container container-fluid"><div class="product-catalog-view view view-phone-catalog view-id-phone_catalog view-display-id-block_phone_catalog js-view-dom-id-b86f8e377d10a5f1f0bb168655a850991721dd19d827dd1aee1e8d6f55ad79d0" data-once="ajax-pager">
  
    
        <div class="view-filters">
      
<form class="views-exposed-form" data-drupal-selector="views-exposed-form-phone-catalog-block-phone-catalog" action="/shop/phones" method="get" id="views-exposed-form-phone-catalog-block-phone-catalog" accept-charset="UTF-8" data-once="exposed-form">
    <details class="filter-block js-form-wrapper form-wrapper" data-drupal-selector="edit-filter-block" aria-describedby="edit-filter-block--description" id="edit-filter-block" open="open">
  <summary role="button" aria-controls="edit-filter-block" aria-expanded="true" aria-pressed="true" style="display: none;">FILTER<span class="show"><i class="fas fa-chevron-down"></i></span>
    <span class="hide"><i class="fas fa-chevron-up"></i></span>
    <span class="summary"></span>
  </summary>
  <div class="form-row">
    



  <fieldset class="js-form-item js-form-type-cshs form-type-cshs js-form-item-brand form-item-brand mb-3">
          <label for="edit-brand--2YqqeNkJSSo">Brand/Family</label>
                    <div class="shop-cshs-field">
  <select data-drupal-selector="edit-brand" class="simpler-select-root form-element form-element--type-select form-select" id="edit-brand--2YqqeNkJSSo" name="brand" style="display: none;">
                  <option value="All" data-parent="" selected="selected">
          All
        </option>
                        <option value="1001" data-parent="">
          Apple
        </option>
                        <option value="1005" data-parent="1001">
          iPhone 10
        </option>
                        <option value="1004" data-parent="1001">
          iPhone 11
        </option>
                        <option value="1003" data-parent="1001">
          iPhone 12
        </option>
                        <option value="1002" data-parent="1001">
          iPhone 13
        </option>
                        <option value="1192" data-parent="1001">
          iPhone 14
        </option>
                        <option value="1134" data-parent="">
          Phone ABC
        </option>
                        <option value="1193" data-parent="">
          Phone DEF
        </option>
                        <option value="1194" data-parent="1193">
          A
        </option>
                        <option value="997" data-parent="">
          Samsung
        </option>
                        <option value="998" data-parent="997">
          A Series
        </option>
                        <option value="1000" data-parent="997">
          Note Series
        </option>
                        <option value="999" data-parent="997">
          S Series
        </option>
                        <option value="1006" data-parent="">
          Huawei
        </option>
                        <option value="1007" data-parent="">
          Oppo
        </option>
                        <option value="1008" data-parent="1007">
          Series 60
        </option>
            </select><div class="select-wrapper select-wrapper--level-0" data-level="0"><select class="simpler-select form-element form-element--type-select form-select" id="edit-brand--2YqqeNkJSSo--level-0"><option value="All">All</option><option value="1001" class="has-children">Apple</option><option value="1134">Phone ABC</option><option value="1193" class="has-children">Phone DEF</option><option value="997" class="has-children">Samsung</option><option value="1006">Huawei</option><option value="1007" class="has-children">Oppo</option></select></div>
</div>
                      </fieldset>




  <fieldset class="js-form-item js-form-type-select form-type-select js-form-item-price form-item-price mb-3">
          <label for="edit-price--mxwdlAvPjPU">Price range</label>
                    
<select data-drupal-selector="edit-price" id="edit-price--mxwdlAvPjPU" name="price" class="form-select"><option value="All">All</option><option value="1">Below Rs. 10,000</option><option value="2">Rs. 10,000 - 25,000</option><option value="3" selected="selected">Rs. 25,000 - 50,000</option><option value="4">Rs. 50,000 - 100,000</option><option value="5">Rs. 100,000 - 200,000</option><option value="6">Above Rs. 200,000</option></select>
                      </fieldset>




  <fieldset class="js-form-item js-form-type-select form-type-select js-form-item-cameras form-item-cameras mb-3">
          <label for="edit-cameras--V80k4YksURU">Number of Cameras</label>
                    
<select data-drupal-selector="edit-cameras" id="edit-cameras--V80k4YksURU" name="cameras" class="form-select"><option value="All">All</option><option value="1">1</option><option value="2">2</option><option value="3" selected="selected">3</option><option value="4">More than 3</option></select>
                      </fieldset>
<div data-drupal-selector="edit-actions" class="form-actions js-form-wrapper form-group container-fluid" id="edit-actions--kg_yrb-6QN0"><button class="btn btn-danger button js-form-submit form-submit" data-drupal-selector="edit-submit-phone-catalog-zwvv2azewoo" type="submit" id="edit-submit-phone-catalog--ZWVV2AZeWOo" value="FILTER">FILTER</button>
</div>

  </div>
</details>
<script>
  function setFilterVisibility() {
    if (window.matchMedia('(min-width: 768px)').matches) {
      // Hide the toggle button and display the filters
      $(".filter-block").attr('open', true);
      $(".filter-block > summary").hide();
    } else {
              $(".filter-block").attr('open', true);
        $(".filter-block > summary").show();
          }
  }

  setFilterVisibility();

  $(window).on('orientationchange resize', setFilterVisibility);
</script>
</form>

    </div>
    
      <div class="view-empty">
      <br>
<img src="https://dialog-dev-bucket.s3.ap-southeast-1.amazonaws.com/s3fs-public/2022-05/no_results.png" width="250px" class="align-center">
<br>
<p class="text-center"><strong class="align-center">Couldn’t find any devices</strong></p>
<p class="text-center">It seems we don’t have any devices with the filters you have selected at the moment. Try using a different set of filters</p>
    </div>    
          </div>
</div>

these products value can be changed and configured any time. I need to execute either one of scenarios any time. what should I do?

1 Answer 1

1
+50

Your NoSuchElementException is in this case correct because the no Results case is not wrapped into an div with the data-once ajax-pager property, which will always result in an Exception.

I might be missing something here, but is there a reason why you do not just do a driver.findElements(By.cssSelector('.buy-now-button')).length > 0 ?

As far as I see the Button gets only displayed for search results, and findElements, other than findElement, does not throw an excepton if no elements are present.

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.