Using stored values in Javascript comparisons with Selenium

After conducting thorough research for the solution to this issue, I have come across numerous complex questions, but none have provided me with enough insight to successfully resolve this problem.
Here is what I am attempting to do:
1. Open a page displaying a potentially large number
2. Obtain the X Path of where that number is located and store it in a variable
3. Use JavaScript to compare the previously stored variable to determine if it exceeds 10; if so, set a new variable as true, otherwise false (default value)
4. Confirm that the variable in step #3 is indeed true

Seems straightforward, doesn't it?

The complication arises here:
During step 3, comparing the variable from step #2 to 10 proves to be challenging based on my current approach.

Why is this happening?

Further details:

<tr>
   <td>open</td>
   <td>http://www.google.com/search?hl=en&q=selenium+verifyEval</td>
   <td></td>
</tr>
<tr>
   <td>store</td>
   <td>/html/body/div[5]/div/p/b[3]</td>
   <td>resultCount</td>
</tr>
<tr>
   <td>storeEval</td>
   <td>var isMoreThan10 = new Boolean(); isMoreThan10 = (resultCount &gt; 10);</td>
   <td>isMoreThan10</td>
</tr>
<tr>
   <td>verifyExpression</td>
   <td>${isMoreThan10}</td>
   <td>true</td>
</tr>

A potential workaround has occurred to me: Expanding the JavaScript code to retrieve and assign the value to a variable there may provide me with the opportunity to utilize that variable within JavaScript effectively. The exact method eludes me - would anyone be willing to offer assistance with this?

However, surely there must be an improved approach available, right? There should be a way to assign a value to a variable in Selenium and subsequently use that variable in JavaScript on the following line, correct?

Answer №1

Figured out the solution. Not only was I performing the storeEval incorrectly by how I set isMoreThan10; I also made a mistake in setting resultCount with the wrong store function (instead of using storeText) and referencing resultCount incorrectly in the storeEval.

Here is the proper way to handle it:

<tr>
<td>open</td>
<td>http://www.google.com/search?hl=en&amp;q=selenium+verifyEval</td>
<td></td>
</tr>
<tr>
<td>storeText</td>
<td>//p[@id='resultStats']/b[3]</td>
<td>resultCount</td>
</tr>
<tr>
<td>storeEval</td>
<td>(storedVars['resultCount'] &gt; 10) ? true : false</td>
<td>isMoreThan10</td>
</tr>
<tr>
<td>verifyExpression</td>
<td>${isMoreThan10}</td>
<td>true</td>
</tr>

Credit goes to AutomatedTester for recommending the use of ternary assignment in storeEval

Answer №2

This problem is quite straightforward to address

<tr>
   <td>storeEval</td>
   <td>var newBoolean = new Boolean(); newBoolean = (resultCount &gt; 10); newBoolean ;</td>
   <td>newBoolean</td>
</tr>

It is necessary to have the desired result at the end. Utilizing a ternary operator would be more efficient as it does not store the outcome in a variable within the JavaScript code being executed.

For example:

<tr>
       <td>storeEval</td>
       <td>(resultCount > 10) ? true : false</td>
       <td>isMoreThan10</td>
    </tr>

Answer №3

Utilizing TestPlan, which is capable of running with both Selenium and HTMLUnit as backend tools, can greatly enhance this task.

Navigate to URL http://www.google.com/search?hl=en&q=selenium+verifyEval
assign %Result% (response //html/body/div[5]/div/p/b[3])
Acknowledgment: Retrieved Value %Result%
Verify if %Result% is greater than 10

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

Ways to update the background shade of tr td elements inside a table

I'm having trouble changing the background color of td elements within my table. The browser keeps giving me an error message that says "Cannot read property 'children' of undefined". Despite this, my JavaScript can still navigate through al ...

Using JavaScript and Tampermonkey to convert strings from an HTML element into an array

I am trying to change the names of months into numbers and place them in a table cell on a website. I thought using an array would make it easier to reference the month names by their corresponding array numbers, allowing me to add table tags before and af ...

Creating a unique WooCommerce product category dropdown shortcode for your website

I am having trouble implementing a WooCommerce categories dropdown shortcode. Although I can see the drop-down menu, selecting a category does not seem to trigger any action. Shortcode: [product_categories_dropdown orderby="title" count="0" hierarchical=" ...

Tips for locating the correct xpath for a menu link

Looking for the correct XPath for the steps below: Go to "" website, Click on the All job functions link. Need an XPath to click on the 'Information Technology (IT)' link. The following XPath worked successfully: "//*[@id='searchbox-jfu ...

AngularJs and graph representation

After attempting to generate charts using angular-chart, I followed all the necessary steps and requirements outlined in the documentation. However, upon completion, my canvas element failed to display any content. My HTML Layout: Here is a snippet of my ...

Explore the comparison feature with jQuery's combobox

I am attempting to compare values from an array with values in a combobox using jQuery, but I am encountering difficulties. My array is structured like this: (value 1, value 2,...) names separated by commas (Example: john smith, peter pan). On the other h ...

Having trouble accessing the loadTokenizer function in Tensorflow JS

As a beginner with Tensorflow.js concepts, I recently attempted to tokenize a sentence using the Universal Sentence Encoder in Javascript. You can explore more about it on Github Reference $ npm install @tensorflow/tfjs @tensorflow-models/universal-sentenc ...

Converting a momentjs datetime stored in MySQL in UTC format to local DateTime format

I have a column in my table named "registerdate" with the data type "datetime" in MySQL. Let's assume the current time is "2015-10-10 06:00:00" in local time. In the database, I am storing UTC time, so it will be converted to "2015-10-10 00:30:00" a ...

Hide the Modal Content using JavaScript initially, and only reveal it once the Onclick Button is activated. Upon clicking the button, the Modal should then be displayed to

While trying to complete this assignment, I initially attempted to search for JavaScript code that would work. Unfortunately, my first submission resulted in messing up the bootstrap code provided by the professors. They specifically requested us to use Ja ...

The Vue app for logging in with Metamask is unable to communicate with the Metamask extension due to a lack of interaction between the store components and the frontend interface

After following a tutorial to create a Metamask login app with Vue, I ran into some issues. The code provided lacked a defined project structure, so I decided to organize it in my own Github repo here. However, despite compiling successfully, the button to ...

Pattern matching for directory path excluding the filename

Looking for assistance with creating a JavaScript regex pattern to validate text input as folder paths only, excluding the filename. Can someone provide guidance on this? For example: folder1/folder2/folder3 => valid folder1/folder2/filename.txt1 =&g ...

Show the new elements added to an array in Angular without the need to refresh the page

I'm facing an issue where data added to an array is not being displayed on the browser, even though I can see it in the console. How can I ensure that the newly added data shows up without refreshing the screen in Angular? user.component.ts UserData: ...

What is the best way to clear an XML or table?

As I delve into learning Javascript, I've been experimenting with different approaches to achieve a specific functionality. I'm trying to implement a button that can toggle or hide XML data in a table. My attempts so far have involved adding anot ...

Activating the Play button to start streaming a link

Recently delved into the world of Ionic 4 and Angular, so definitely a beginner :) Purchased a UI Template from code canyon but didn't realize I needed to code the music player part. Been trying to get a music stream playing but no luck. Came across ...

Error in jQuery when element is not found

On my website, I use multiple jQuery functions, but not all of them are necessary on every page. These functions are all located in one XXX.js file, such as: jQuery(function() { $(".title").slug({ slug:'slug', hide: false }); }); ...

What could be causing my Angular.js controller to run its function twice in this code snippet?

Currently, I have an ng-repeat in place, looping through an array of objects that are associated with the SomeController: <div ng-controller="SomeController as aCtrl"> <div ng-repeat="category in aCtrl.categories"> <p ng-show="aCtrl.c ...

Broadcast signals to an overarching frame

I have successfully embedded a chatbot (Angular 14 app) in an iframe and now I need to determine whether the frame should be minimized so it can fit within the parent container. My goal is to send custom events to the receiving frame. let iframeCanvas = do ...

Python code for extracting data from a website using a "Load More" button

Looking to extract links for 675 products from a website. The initial page displays only 24 products with a "Show Next 23" button. I attempted two methods to load additional products in order to grab their links. from selenium import webdriver from seleniu ...

Obtaining the dynamic id with xpath in Selenium WebDriver

I am having trouble retrieving the id of an element that starts with 'u_' and ends with '_5', with varying data in between. Sometimes it appears as 'u_d_5' while other times it is 'u_6_5'. The usual methods like star ...

Comparing JSON objects using Javascript and AngularJS

In the page I'm working on, there are several input fields where users can enter data such as text boxes and dropdowns. When a user fills in the data and clicks SAVE, certain checks and manipulations need to be done before the actual saving process st ...