Using Selenium and JavaScript to store the result of getText() in a variable seems to be challenging

Working on testing with Selenium and Chrome.

I have a JavaScript array called dates with two elements, an empty array named allDates, and the following code snippet:

    dates.forEach(async date => {
      console.log("🚀 ~ file: runSelenium.js ~ line 42 ~ position", await date.getText());
      let newDate = await date.getText();
      console.log("🚀 ~ file: runSelenium.js ~ line 44 ~ newDate", newDate)
      allDates.push(newDate);
    })
    console.log("🚀 ~ file: runSelenium.js ~ line 47 ~ allDates", allDates)

However, the output appears as follows:

🚀 ~ file: runSelenium.js ~ line 47 ~ allDates []
    ✓ First test (678ms)
🚀 ~ file: runSelenium.js ~ line 42 ~ position 26-02-2021
🚀 ~ file: runSelenium.js ~ line 42 ~ position 06.11.2020

It's strange that the last console.log appears first. The main issue is that the second console.log (the one with line 44) is missing completely.

I attempted different ways to assign data to the newDate variable such as:

let newDate = (await date.getText()).toString();

let newDate = await date.getAttribute('value');

let newDate = (await date.getAttribute('value')).toString();

Unfortunately, none of the above approaches worked.

So, I have a few questions:

  1. How can I adjust this to successfully store the newDate variable in an array?
  2. Why isn't the console.log referencing line 44 displayed, even when it should be empty?
  3. What could be causing the incorrect order of console.logs and the test name?

Answer â„–1

Opt for a for-loop over using forEach

for (const day of days) {
  console.log("🌟 ~ file: runAutomation.js ~ line 22 ~ position", await day.getText());
  let newDay = await day.getText();
  console.log("🌟 ~ file: runAutomation.js ~ line 24 ~ newDay", newDay)
  allDays.push(newDay);
}

console.log("🌟 ~ file: runAutomation.js ~ line 27 ~ allDays", allDays)

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

Utilizing XmlHttpRequest and Pure JavaScript to Trigger WebMethods

Struggling with an apparently simple task that has left me completely stumped. After exhausting all my research efforts, I've hit a dead end and decided to seek help from the community. Let me outline the issue: An ASPX page (Q2.aspx) decorated wit ...

Storing numerous data entries within a single attribute in a database

Can you help me retrieve values from the input fields and store them in my database? When I execute this code, the table displays "array" instead of the entered values. Javascript function to add input field: <script> $(document).ready(function() { ...

Exploring AngularJS and Jasmine: Testing a controller function that interacts with a service via $http

I encountered an issue while testing a controller that relies on a service. The problem arises because the service is currently set to null in order to focus solely on testing the controller. The current test setup is failing due to the BoardService being ...

Using jQuery for client-side validation when the user clicks a button in an ASP.NET application

My goal is to implement field validation on button click using Jquery, but I'm facing an issue where it seems like the code behind event and the jQuery event are executing simultaneously. (UPDATED) <asp:Button ID="btnsave" runat="server" ClientI ...

It appears that the event listener attached with the ".on()" method has suddenly ceased functioning

Starting off, here is a link to my project on jsfiddle I will discuss how it's supposed to work and the main issue I am facing. The project consists of three "lines" represented at the top by a selector box. Each line has different "parts" displayed ...

Running tests in Selenium with Java using the @Factory annotation encountered an issue

Let me outline the structure of my class: package com.gex.base.helper; public class InitializeDriver extends BrowserFactory { HashMap<String, String> authenticationMap; @Factory(dataProvider="authentication", dataProviderClass=DataProvid ...

Show the Angular table header only once, and automatically hide it if there is no data available

I am currently working with Angular 5. My goal is to show the table header only once when the typeId is equal to 3. Additionally, if none of the data items have a typeId equal to 3, I want to hide the entire table along with the header. Below you will fi ...

The issue of Lost UI Shared Context is being displayed by ChromeDriver

My Windows 10 laptop is currently running a Windows 7 Virtual Machine. When I try to start the WebDriver inside the Virtual Machine, I encounter an error message that says gpu_process_transport_factory.cc<1009> Lost UI shared context: IWebDriver dri ...

Trilateration of three-dimensional points using K vertices on each face

I'm currently working on a project using Three.js and I have a set of 3D points in the form of (x, y, z) coordinates, as well as a series of faces. Each face consists of K points and can be either convex or concave in shape. Despite consulting the doc ...

Scraping dynamic elements with Selenium in Python

When extracting information from an SEC archives website, there are several key details to look for: Large accelerated filer Accelerated filer Non-accelerated filer Smaller reporting company Emerging growth company The website represents this data as fo ...

The error "Response.json() is not a function" is suddenly being raised, even though there were no issues before and no changes have been made

I am in the process of developing a React website and encountering an issue with calling an API that I have created. Initially, the fetch request was working correctly. However, after adding a call to a PUT mapping which executed successfully, I found th ...

Having Trouble Extracting HREF Link from Instagram Time Tags Using Python Selenium

PostLinkExtraction = driver.find_element_by_xpath("//article[1]/div[3]/div[1]/div/div[2]/div[1][*[local-name()='a']]").get_attribute('href') print (PostLinkExtraction) I am attempting to extract the href link from the Time St ...

Querying Denormalized Data in AngularFire 0.82: Best Practices and Strategies

I have a question that is related to querying denormalized data with AngularFire. I am looking for a solution specifically using AngularFire (current version 0.82). Here is an example of the data structure I am working with: { "users": { "user1": { ...

accurate JSONP reply

Currently, I am experimenting with JSONP locally to receive a correct response and pass it into my callback function jsonp_callback. I am referencing code from: How do I set up JSONP? header('content-type: application/json; charset=utf-8'); $dat ...

Patience is key as you wait for a link to materialize on the page within the Selenium environment

Hey there, I'm currently using Selenium with TestNG for my project. The code I've created involves logging into a website and then performing an operation that collects the names of all IP addresses within a given range. After scanning, a link la ...

Battle of Kingdoms API ajax

When attempting to access Clash of Clans API information in this script, the following error is encountered: Refused to execute script from 'https://api.clashofclans.com/v1/leagues?authorization=Bearer%20eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiIsImtpZCI6Ij ...

Verify whether the current directory includes a package.json file with a matching name value

When I run a custom command in the terminal, I am attempting to achieve two objectives: Verify if there is a package.json file in the current directory (similar to checking for the existence of process.cwd() + '/package.json'). Determine if the ...

What are the possible reasons for a checkbox not being checked in React JS?

I've been working with react final form and I'm encountering an issue where I can't seem to get the checkbox to be properly checked. I'm not sure what mistake I might be making. Take a look at my code on CodeSandbox const AppWithIconTo ...

Is there a way to transfer an image from background.js to background.html?

I'm in the process of creating a chrome extension that involves making an API call every hour to retrieve an image, which I then want to save in chrome.storage.local. However, the size of the image is quite large, so I'm resizing it using a canv ...

Can I use my own custom PayPal button with Express Checkout and REST API?

I have successfully integrated PayPal REST API and am now able to create payments. The next step is to enable payment approval. I followed the documentation found here: https://developer.paypal.com/docs/integration/web/accept-paypal-payment/ It seems tha ...