Is there a "hasContent" function available in webdriverjs?

Currently, I am exploring the use of webdriverjs for testing purposes and I am really enjoying the experience so far. However, I have encountered a challenge while attempting to run more general tests. I am wondering if there is a way to check if a webpage contains specific content without having to specify a particular CSS selector?

For example, suppose I have a webpage with text scattered all over the place and I want to verify if the string "Hello World!" is present somewhere on the page. The exact location of the string is not important, just its presence.

If achieving this with webdriverjs is not feasible, is there an alternative method to conduct such a test?

Answer №1

Using webdriverjs, you can achieve this functionality. Although there is no built-in hasContent method, you can easily create one by utilizing the getText() method. This method retrieves the visible text of a specified element and its child elements. To extract the entire text content of a webpage, simply invoke it on the <html> element, like so:

var selenium = require('selenium-webdriver');
var By = selenium.By;  
var driver = new selenium.Builder().withCapabilities(selenium.Capabilities.chrome()).build();

function hasContent(content) {
    driver.findElement(By.tagName('html')).getText().then(function(entireVisibleText) {
        if (entireVisibleText.indexOf(content) > -1) {
            // Content found
        }
        else {
            // Content not found
        }
    });
}

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

Obtain the Xpath for the element with a certain tag located closest to the given element in its immediate surrounding

Within my HTML document, I have a specific element with an id of uniqueId. There are multiple nested tags surrounding this element, and I am trying to retrieve a particular tag that is closest to it. For example: <div> <div> ...

The count of find_elements_by_xpath is not accurate

I am on a mission to retrieve the total number of games in the 2023-24 Regular Season table from the following link. My goal is to populate the variable total_games with these elements. However, I am encountering an issue when I try to print the length us ...

Having trouble generating an image with JavaScript

I am currently working on incorporating an image onto a webpage using JavaScript. Surprisingly, even the alert('This function works!') is not displaying anything! What could be causing this issue? Please assist! <!DOCTYPE html> <html> ...

<JavaScript> changing the color of hyperlink in d3.js - <Organizational Chart>

screenshot The width of the link can be adjusted, but the color remains unchanged. I have attempted various solutions series.links.template.setAll({ strokeWidth: 2, strokeOpacity: 0.5, color: am5.color('#ffffff'), links: ...

What is the method for determining the new point coordinates and direction vector after a rotation of degrees in three.js?

How can I determine the coordinates of point A to H after rotating a certain number of degrees and aligning them with direction vector (-42, 51, 11) using three.js? Thank you in advance for your help and please forgive any mistakes in my English. Best reg ...

Generating HTML using a filter

I have been working on creating a filter that will render HTML tags. Here is the code for my filter: filters: { limitStrLength: function (value, maxLength) { if (value && value.length > maxLength) { let partialVal = value.substr(0, ...

Separate string by using a regular expression pattern

Looking to parse a dynamic string with varying combinations of Code, Name, and EffectDate. It could be in the format below with all three properties or just pairs like Code-Name, Code-EffectDate, or Name-EffectDate. {"Code":{"value":"1"},"Name":{"value": ...

Attaching dynamic data to a specific element within an array

I have successfully created a demo where elements can be dropped into a specific area and their top and left values are displayed. I have also added functionality to remove dropped items and move them between different blocks. However, I am encountering so ...

Determine whether the object is facing the specified position

I'm attempting to verify whether an object (this.target) is facing towards a particular position (newPosition). Here's what I currently have: new THREE.Matrix4().lookAt( newPosition, this.target.position, this.target.up ) == this.target.matrix ...

Developing a modular text input component with Material UI and react-hook-form for enhanced reusability

While I was in the process of creating a reusable text field component using Material UI and react-hook-form, I decided to refer to a couple of examples for guidance: Example 1 source: type FormProps<TFormValues> = { onSubmit: SubmitHandler& ...

Having trouble displaying results in Vue.js after making an API request?

I am facing challenges in displaying the results using vue.js. The data from my API (ASP.NET CORE) is being retrieved successfully, as shown in my vue dev tools on Google Chrome. However, I am encountering difficulties in rendering the results on the brows ...

A comprehensive guide on implementing filtering in AngularJS arrays

I am working with the array provided below: [ { Id: 1, Name: "AI", Capacity: 2, From: "2021-10-27T08:00:00", To: "2021-10-27T08:50:00", }, { Id: 2, Name: "TEST", Capacity: 2, ...

jquery technique for toggling a single button

My goal is to use jQuery to toggle a button rather than the typical paragraph toggling. I want the button to appear and disappear when clicked, while also increasing the "score" upon each click and decreasing it when the button reappears. Can anyone guide ...

Guide on implementing router-link within a select option dropdown in a Vue.js application

a.vue <template> <div>hi i am component 1</div> </template> <script> export default { name: "a", }; </script> b.vue <template> <div>hi i am component 2</div> </template> <script> ...

The XPath selector used in Selenium Java is throwing an InvalidSelectorException due to a SyntaxError with an unexpected token "}"

I am attempting to select the final span element in the provided HTML below <div class="rating rating-set js-ratingCalcSet" > <div class="rating-stars js-writeReviewStars"> <span class="js-ratingIcon glyphicon glyphicon-star fh">& ...

Issue encountered in the file located at ./node_modules/framer-motion/dist/es... where the export 'useId' (imported as 'useId') could not be located in 'react' along with 'useInsertionEffect'

After adding the framer-motion library using yarn add framer-motion, I encountered the following configuration: https://i.sstatic.net/rG2FJ.png Error message : ERROR in ./node_modules/framer-motion/dist/es/components/AnimatePresence/PopChild.mjs 40:13- ...

Why does variable passing use 'object Text' instead of passing values?

In my for loop, I am dynamically creating a table with radio buttons and trying to create labels dynamically as well. However, when I pass the variable to the label text node, it prints out 'object Text' on the page instead of the expected value. ...

Issue with Ajax submit button failing to pass ID

I am encountering an issue while trying to update data using bootstrap modal. When the CGridView selectionChanged event is triggered, it should call a function to display a modal dialog form and populate the form with the selected data. Below is my CGrid ...

To trigger OnClick events in Next.js SSG, you will need to double click the element for it to run properly

There seems to be an issue where elements with an onClick event listener require a second click to run. It appears that the state is not updating on the initial click, causing this behavior. This problem may be related to using getStaticProps() for SSG on ...

Different approaches for expanding object.prototype while utilizing jQuery

Once upon a time, I made the mistake of trying to extend Object.prototype and ended up encountering errors in my jQuery file. After doing some research, I found out that extending Object.prototype is frowned upon in the JavaScript community due to potentia ...