Retrieve the content of the specified element within the webpage

How can I modify the method to successfully retrieve the text content of an element on a webpage using Selenium with JavaScript? Currently, it is returning undefined.

homepage.js

const { Builder, By, Key, until } = require('selenium-webdriver');
let Basepage = require('../pageobjects/basepage');
    
class Homepage extends Basepage {
    
 isAviasales(){
         driver.findElement(By.xpath("//h1[@class='header__title']")).getText().then(function(txt){
             return txt+'';});
        }
module.exports = new Homepage();

basepage.js

    let webdriver = require('selenium-webdriver');
let driver = new webdriver.Builder().forBrowser('chrome').build();
driver.manage().setTimeouts( { implicit: 10000 } );

class Basepage{
    constructor(){
        global.driver = driver;
    }

   go_to_url(theURL){
       return driver.get(theURL);      
    }
}

module.exports = Basepage;

testpage.js

const assert = require('chai').assert;
const homepage = require('../pageobjects/homepage');
let baseurl = 'https://www.aviasales.by/';

describe('Tests are starting', function(){
      this.timeout(7000);
   
    it('Mainpage test',function(){
        
        homepage.go_to_url(baseurl);
     // assert.equal(homepage.isAviasales(),'Поиск дешёвых авиабилетов');
    })

    it('Omsk test',function(){                     
        homepage.enterText('Омск');
      //  assert.equal(homepage.isOMS(),'OMS');
    })

    it('Swap test',function(){                
       homepage.clickSwapPlaces();
    })
})

MoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetailsMoredetails

Answer №1

Seems like there's a missing delay required here.
It appears that the element you're attempting to reach hasn't fully loaded on the webpage when you're trying to access and retrieve its text content.
For a more accurate solution, please provide us with the entirety of your code.

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

Refreshing an online page

Help needed with creating a function to refresh any page using its URL. My code is not running as expected. Here's what I have: from selenium import webdriver import time def page_refresh(url): driver = webdriver.Firefox() driver.get(url) ...

Ways to specifically load a script for Firefox browsers

How can I load a script file specifically for FireFox? For example: <script src="js/script.js"></script> <script src="js/scriptFF.js"></script> - is this only for Firefox?? UPDATE This is how I did it: <script> if($. ...

Make the adjustment from an H1 tag to an H2 tag with the help of

I need assistance with changing the HTML code from using an <h1> tag to a <h3> tag, using Vanilla JavaScript. Here is the code snippet in question: <h1 class="price-heading ult-responsive cust-headformat" data-ultimate-target=" ...

What is the proper usage of main.js and main.css within the skeleton portlet project that is created by the Liferay 6.0.6 plugin SDK?

Is it a good practice to include portlet-specific JS or CSS in them only if <portlet:namespace /> works within them? Should I rely on unique function/variable names or class names instead? ...

Experiencing a problem with XAMPP? Changes made to the code are not reflected after saving

Recently, I've encountered a strange issue with my XAMPP test server while working on a game project. Everything was running smoothly until I noticed that when I make changes to certain files in Notepad++ and save them, the updates are not being refle ...

I'm experiencing an issue where my JavaScript function is only being triggered

I have a simple wizard sequence that I designed. Upon selecting an option from a dropdown menu on the first page, a new page is loaded using jQuery ajax. However, when clicking back to return to the original page, my modelSelect() function, responsible for ...

Unraveling the complexities of double-encoded UTF-8 in PHP

Trying to transmit data from an HTML page via Ajax to a PHP page. This is the jQuery code I'm using: $.ajax({ url: "test.php", type: "POST", data: { name: "João" } }).done(function (data) { alert(data); }) When sending ...

Displaying a submenu upon hovering within its designated CSS region

I'm encountering an issue with my submenu. It's supposed to appear when hovering over the parent menu li, but it also shows up when the mouse hovers over its area. Let's take a look at some images. First screenshot below shows that it works ...

Using JavaScript to issue a Windows authentication request for Kerberos

I have created a web API with Windows authentication enabled (using IIS as well). The issue arises when my client attempts to send an AJAX request to the web API, resulting in a 401 unauthorized response. Upon further investigation, it appears that the pr ...

Avoiding redundancy by establishing the loading state in a redux reducer

Let's dive into a concrete example to better illustrate my point. In the webapp I'm working on, users can apply for jobs using a job reducer that handles various actions such as creating_job, created_job, fetching_job, fetched_job, fecthing_jobs, ...

Error in TypeScript - Anticipated 1-2 arguments, received either none or multiple. Code Issue TS2556

While working in JavaScript, I had no issues with this code snippet. However, when I attempted to implement it in a TypeScript Project, an error surfaced. The problem seems to revolve around the fetch(...args) function call. const fetcher = (...args) =&g ...

Content Security Policy Error triggered by Iframe Source Running Script in Web Extension

My web extension for Firefox utilizes a content script to add HTML to a webpage when a button is clicked. The injected HTML includes an iFrame nested in multiple div elements. Below is the relevant part of the content script: var iFrame = document.create ...

Steps for eliminating an element using jQuery from a variable filled with HTML code

I'm developing a forum where users have the ability to quote other users' comments. When a user clicks on "quote" for a comment, it captures the HTML of that comment and displays it in a box that says Quote: Original post by a member, similar t ...

Warning: ComponentMounts has been renamed. Proceed with caution

I'm encountering a persistent warning in my application and I'm struggling to resolve it. Despite running npx react-codemod rename-unsafe-lifecycles as suggested, the error persists and troubleshooting is proving to be challenging. The specific w ...

"Utilize Vuejs to establish a binding between two objects when necessary

With the help of moment, my calendar generates 41 days. for (let x = 0; x < 42; x++) { context.add(1, 'd'); let day = { 'date': moment(context), 'events': [] }; } ...

The forEach method in JavaScript seems to work asynchronously

After reviewing other discussions on this platform, it seems that the general agreement is that forEach should be synchronous and block. However, in my code, something appears to be off as it doesn't behave that way: var noDupes = false; // se ...

When setting an attribute on load, Javascript may not properly apply the change to the attribute

I designed a webpage where images have a fade-in effect using a CSS class. Initially, 4 images load with the desired effect. However, I encounter an issue when trying to apply the same effect to a new image that appears upon clicking a button. Below is th ...

Troubleshooting issue in App component while utilizing React-Redux's 'connect' function

Upon attempting to utilize the connect() function provided by react-redux, I encountered the following error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. Check the r ...

Error: Unspecified process.env property when using dotenv and node.js

I'm encountering an issue with the dotenv package. Here's the structure of my application folder: |_app_folder |_app.js |_password.env |_package.json Even though I have installed dotenv, the process.env variables are always u ...

Issue with NPM identifying my module (demanding unfamiliar module)

Currently, I am developing a React-Native application and organizing my components into separate files. Most of the time, this method works perfectly fine except for one specific instance where I keep encountering a 'Requiring unknown module' err ...