Guide on waiting for the basket count to transition from zero to one after adding products to the basket in Selenium with Java

I'm currently working on automating the process of adding products to a basket on Amazon and verifying if the basket count changes after items have been added.

To achieve this, I am using explicit waits to ensure that the products are added properly. However, since the basket icon is already present on the page, my code is having trouble retrieving the basket count after adding a product.

WebDriverWait waitDriver = new WebDriverWait(driver,30); waitDriver.until(ExpectedConditions.visibilityOfElementLocated(By.id("nav-cart-count")));

Answer №1

In the scenario where you are transitioning from no items to some items in your basket, a possible solution is to wait for the appearance of the class nav-cart-1, which only shows up when there are items present.

WebDriverWait waitDriver = new WebDriverWait(driver,30); 
waitDriver.until(ExpectedConditions.visibilityOfElementLocated(By.css("span#nav-cart-count.nav-cart-1")));

However, if you require an exact count, this approach may not be sufficient.

To accurately verify the count, it is recommended to utilize xpath with the text() condition

    waitDriver.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text() = '1' and @id='nav-cart-count']")));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Selenium WebDriver in Java to click on the Facebook sharing button for the specific post I am interested in

I'm struggling to locate and click the share button on a Facebook post. Let's say I have 4 posts displayed on my Facebook page, and I want to share one of them by clicking on 'Share' and then selecting 'share...' from a small ...

Can Selenium be utilized to interact with a button in a Google Slides Presentation?

Within Google Slides, there is a feature located under tools >> Linked Objects known as "Update all": https://i.stack.imgur.com/mbXoG.png This function updates all elements in the Presentation. Although Google App Scripts can achieve something simi ...

Encountering issues with DesiredCapabilities in Chrome when using Selenium 3.0

package com.sb.testpackage1; import org.openqa.selenium.Platform; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; ...

Error encountered during fresh installation: nullpointerexception

After encountering this error, I decided to do a fresh installation of Eclipse. Despite deleting all the files, it seems like I missed a few. The error message is as follows: java.lang.NullPointerException at testing.testsrc.testcase1(testsrc.java:16) at ...

What is the best way to extract information from a website that is dynamically generated using JavaScript instead of directly coded in HTML?

I'm attempting to scrape the content found on website. This particular website is dynamically generated using JavaScript to update the DOM tree. I've made an attempt at using Selenium code to extract elements from tables, but unfortunately, I&a ...

Can a piece of code be created that generates the XPath for a specific element?

I'm interested in finding out if there is a way to automatically generate an XPath by simply selecting an element, eliminating the need for manual updates when changes occur in HTML files on websites. I welcome any suggestions or alternatives that can ...

WebDriver: maximizing efficiency by running tests concurrently

On my system, there is a Web-interface that requires login/password authorization. I am looking to develop an automated test where 100 tests are run simultaneously. Each test will utilize a unique username and password combination, such as user1/PaSsWord1, ...

Selenium retrieves null value from PageFactory

In my project, I have a driver class called Hooks.java, a class for login page objects named LoginPage.java, and a step definition class. However, I am facing an issue where my page factory object is not initializing in the login class, resulting in a null ...

A guide on ensuring the favicon is fully loaded before proceeding with Selenium Webdriver

Is there a way to determine if a page has fully loaded using the Favicon as a reference point? I believe that since the Favicon is typically the last element to load on a webpage, it could be used as a reliable indicator of complete page loading. Instead ...

Choosing an option from a drop-down list using Selenium WebDriver

I'm encountering an issue where I can't seem to select a value from a dropdown using selenium webdriver - the dropdown opens but immediately closes. Here is the webdriver code snippet: driver.findElement(By.cssSelector("span.k-select")).click() ...

Element with specified ID cannot be found

After exploring various related topics, I am still unable to find a solution that works. On my website, there is a PDF document generated with a download link that I am trying to interact with. However, Selenium keeps returning errors stating it cannot loc ...

Attempting to trigger a button click in Selenium Webdriver utilizing Cucumber-JS

Today, I spent the entire day attempting to click a button in an automated test using various selectors (id, class, and href) but to no avail. It seemed easier with the ID selector, however, after practicing on YouTube and successfully clicking the hamburg ...

Error encountered during execution of For-Each loop using VBA and Selenium?

I encountered an issue with the error message "Object doesn't support this property or method." Here is the code snippet that triggered the error: Dim webs, web As Object Set webs = driverg.findElementByCssSelector("*[class^='title-link id-trac ...

Unable to locate element through xpath with selenium

I have attempted to use this xpath, but I am unsure of how to proceed. I have two objects in a popup menu and I need to select the first one. Here is the HTML code for the page: </div> <input class="sprite form-enter" type="submit" value="" name ...

WebElement can be seen at times and at other times cannot be seen

Occasionally, the element may appear and disappear intermittently. Furthermore, the element is not currently in the Document Object Model (DOM). What would be the best approach to manage this scenario with Selenium Webdriver? ...

Interact with the button inside an IFrame using Selenium IDE to acknowledge cookie usage

I am trying to automate clicking on a button within an iframe using the Selenium IDE extension for FireFox. Unfortunately, the "switch to frame" command is not working as it cannot detect any iframes. An example of a website that has a similar cookie dial ...

Java code for Selenium WebDriver to wait for cookies and local database cookies

In Java, I utilize Selenium to access cookies from the local database. However, when using the following code snippet to wait for the site to be fully loaded, I do not receive all the cookies: webDriverWait.until(webDriver -> "complete".equals ...

Nightwatch.js feature not functioning properly in a 'closing' manner

I'm facing an issue where I need to execute a function at the beginning of my test before proceeding with the rest of the test steps. Here is the custom command I am using, named internalAdviceLinksHtml: const solr = require('solr-client') ...

The search box on eBay is mysteriously elusive when using Python and Selenium

I've been attempting to use Python's Selenium library to click on the description box for selling items on eBay, but it seems unable to locate it. I've also tried switching to frames, but without success either. Here is the code snippet I am ...

When using WebDriverWait along with expected_conditions as element_to_be_clickable in Selenium Python, the __init__() function received 3 arguments instead of the expected 2

I have encountered a problem where I do not have an "init" function in my code, unlike similar questions. How can I resolve this issue? The specific problem lies with the line (EC.element_to_be_clickable) from selenium.webdriver.common.by import By from ...