Unable to retrieve list using Selenium in C# due to issues with FindElements method

Currently, I am working with C# and NUnit.

I have encountered an issue where I need to extract the value from a span element and store it in an ArrayList.

This is the code I am using to retrieve the price list:

var productprice = driver.FindElements(By.CssSelector(".price.mb-10.text-right "));

from the HTML snippet provided below:

    <div class="pl-sec">
<ul>
<li class="row-fluid">
<li class="row-fluid">
<div class="span2">
<div class="span7 disc-content">
<div class="span3 price-content">
<div class="price mb-10 text-right"> £14.99</div>
<div class="full-width mb-20">
</div>
</li>
<li class="row-fluid">
<div class="span2">
<div class="span7 disc-content">
<div class="span3 price-content">
<div class="price mb-10 text-right"> £16.99</div>
<div class="full-width mb-20">
</div>
</li>

While I am able to retrieve the list, I am struggling to fetch the text from the elements obtained using the CSS selector.

Here is what I see in Visual Studio:

    -       [0] {OpenQA.Selenium.Firefox.FirefoxWebElement} OpenQA.Selenium.IWebElement {OpenQA.Selenium.Firefox.FirefoxWebElement}
+       [OpenQA.Selenium.Firefox.FirefoxWebElement] {OpenQA.Selenium.Firefox.FirefoxWebElement} OpenQA.Selenium.Firefox.FirefoxWebElement
        Displayed   true    bool
        Enabled true    bool
+       Location    {X = 802 Y = 793}   System.Drawing.Point
        Selected    false   bool
+       Size    {Width = 164 Height = 14}   System.Drawing.Size
        TagName "div"   string
        Text    "£16.99"    string

I am trying to extract the 'Text' containing the price shown in the above output.

Apologies if my explanation is unclear, as I am relatively new to this concept.

Answer №1

Successfully Resolved!

The problem arose from the use of a variable that stored elements as an arraylist and required locating the position before accessing the Text property. Here is the updated code snippet-

    driver.FindElement(By.XPath("//div[4]/div[2]/ul/li[2]/label")).Click();
        //This section calculates the count of products displayed by validating the price.
       var productprice = driver.FindElements(By.CssSelector(".price.mb-10.text-right "));
        //var productprice = driver.FindElement(By.XPath("//div[@class='price.mb-10.text-right']")).Text;

       string strval = "";
       strval = productprice[5].Text;

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

The headline box is displaying CSS code instead of the content. How can this issue be resolved?

Having an issue with a WordPress template that features a blue "headline" box that I need to change the color of to #333399. I tried inspecting the element, browsing through the code, and identified the code below as what needed modification: #headline, # ...

Having trouble opening HTTPS pages in Google Chrome using Selenium with C#?

Trying to direct the Selenium to a secure https page, but having trouble as the Chrome browser is not redirecting to the URL. If I use an http URL, it works fine. Does anyone have any suggestions for resolving this issue? This is the code snippet I am us ...

Mastering the 'Down Arrow' action in Java Webdriver

After spending countless hours trying to figure this out, I am completely stumped. The issue revolves around a type ahead field with the following code: <input type="text" id="id_attendees" name="attendees">. Typing in this field triggers a JavaScrip ...

Performing a series of SQL queries in order to fill out the contents of

I am currently working on populating a summary table from a MySQL database, which consists of multiple rows and cells. Here is the image of my table: I initially thought about writing individual SQL queries for each cell, but this approach would require a ...

Spacing between products in Woocommerce product list

Welcome to my website! Check it out here: I am facing an issue with a long margin at the bottom of my woocommerce product list. I have tried using CSS to change it as shown below: .woocommerce .products ul, .woocommerce ul.products { margin-bot ...

Incorporate pug and sass into your React project for a more

Are pug/jade and sass compatible with react? I enjoy organizing my code by separating files, and the functionality that sass and pug provide is great for this purpose. Is there a way to integrate pug and sass into a react project? Pug example: doctype ...

Is there a way to access the userID in the React.js front-end?

As I work on completing my Full Stack Web app with JWT token authentication based on roles, I find myself facing challenges with the front-end development. Specifically, I am unsure of the best practice for retrieving the UserID in the front-end to facil ...

Pattern for identifying Google Earth links with regular expressions

I'm attempting to create a regular expression that can validate whether the URL entered by a user in a form is a valid Google Earth URL. For example: https://earth.google.com/web/@18.2209311,-63.06963893,-0.67163554a,2356.53661597d,34.99999967y,358.13 ...

The dimensions of the HTML table have expanded excessively following the execution of mysql_fetch_assoc

I have encountered an issue where, after inserting the database values into an HTML table, the table becomes too large. I've tried using CSS code to adjust it, but it doesn't help. Should I consider changing something in the database value type? ...

I only notice certain text after carefully inspecting the elements in Google Chrome

While creating an application on Sitefinity (ASP.NET), I encountered an issue where certain elements, such as button text and labels, were not loading correctly when running the application. However, upon inspecting the element, they appeared to be working ...

Unable to retrieve the value from an "input" element

Currently, I am utilizing Selenium with Java to create a test. My task involves retrieving the text from inside an input element: <table class="boundaryFormAdd"> <tbody> <tr> <td> <input id="id ...

"My JavaScript code for toggling visibility is not functioning properly. Any suggestions on how to fix

I am currently working on a task that involves showing and hiding container1, but I am confused as to why my code is not functioning properly. The task requirements are as follows: - Clicking on the "Show" button should display container1 - Clicking on the ...

Is it time to switch out HTML tables for Divs?

I am looking for advice on creating a consistent header for all of my website pages. Currently, I use a div element to wrap a table that contains one row with three cells. These cells hold three images: one in the top-left corner, one in the top-center, a ...

What causes ngb-tabset to reset to the first tab upon being hidden and then shown again?

I have implemented ngb-tabset within a component that is contained within a single div. This div element is conditionally displayed based on the value of a specific condition. If the condition evaluates to false, another section is shown instead. <div * ...

guide on launching react with pure javascript

Is it feasible to operate react "straight out of the box" using only JavaScript? In essence, I am seeking a way to utilize react by simply utilizing notepad to create the page (without needing to install and configure node etc.). More specifically - 1) ...

CSS - a collection of hyperlinks to browse. the pseudo-element a:visited::before is not behaving as anticipated

Here's what I'm looking to achieve: http://codepen.io/anon/pen/KaLarE I want to make a list of links, each with a checkbox in front. Then, I want to be able to check the boxes for visited links. I attempted this approach, but unfortunately, it ...

Select a color by inputting the type as "color" while leaving a space between the border and

I'm having an issue with the color input There seems to be a gap between the black border and the actual color, but I want to get rid of it #color-picker { border: 5px solid black; border-radius: 5px; width: 207px; height: 60px; padding: ...

Checkbox: Activate button upon checkbox being selected

On my webpage, I have a modal window with 2 checkboxes. I want to enable the send button and change the background color (gray if disabled, red if enabled) when both checkboxes are selected. How can I achieve this effectively? HTML: <form action="" me ...

Angular 6: Issue with displaying data on the user interface

Hello! I am attempting to fetch and display a single data entry by ID from an API. Here is the current setup: API GET Method: app.get('/movies/:id', (req, res) => { const id = req.params.id; request('https://api.themoviedb.org/ ...

Utilize Google Tag Manager to Safely Gather Specific Data

My current project involves capturing values from a <select> element, sending them to the Data Layer, and then forwarding them to an Analytics account via Google Tag Manager. The source code I'm trying to extract values from includes a dynamic s ...