Storing Selenium WebElement in memory for extended periods to retrieve its details: A guide in C#

I am using Selenium and C# to monitor a website for any new items that may appear. The website contains a list of <div class="myClass"> elements displayed in a table format with multiple sub-divs. Periodically, I scan all the displayed divs to check if any new ones have been added. However, the number of divs is limited and when a new element appears, the oldest one is removed, causing issues in my code.

For example: The maximum number of items allowed is 3.

  1. I currently have 3 items A,B,C.
  2. A new item D shows up.
  3. As I read the 3 displayed items, another new item E appears while I'm reading the sub-divs of D. This causes D to move and triggers an error saying "Element is no longer attached to the DOM."

This error occurs because it takes too long to read all the sub-divs of D.

I obtain the webelement using FindElement(By.Name("elemName"). Is there a way to retain the element in memory even if it gets moved or removed from the DOM?

Thank you!

Answer №1

Dealing with dynamic changes in WebElement objects can be a real challenge. I remember encountering this issue before, where my list of elements would unexpectedly update based on the page content.

In response to your query, I'm uncertain if it's feasible to retain the WebElement itself without replacing the old div. An alternate approach could involve creating a custom Element class to store the WebElements and essentially create a personalized cache of elements.

Consider this example:


public class Element
{
    public string TagName { get; set; }
    public string Text { get; set; }
    public bool Displayed { get; set; }
    // any other attributes you wish to include
}

public List<Element> StoreWebElements(List<IWebElement> seleniumElements)
{
    var result = new List<Element>();
    foreach (var elem in seleniumElements)
    {
       result.Add(new Element { TagName = elem.TagName,
                                Text = elem.Text, 
                                Displayed = elem.Displayed
                               };
    }
}


// your code
var divList = Driver.FindElements(By.Name("elemName"));

// "store" the elements
var cachedElements = StoreWebElements(divList);

// manipulate the page to make changes
// divList will update, while cachedElements remain unchanged.

While this solution may seem unconventional, personally I have not found a more effective workaround for the described issue. It would be interesting to discover if others have alternative explanations or solutions to offer.

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

Why isn't the click function in Java Selenium working as expected?

What could be causing the issue with the click() function not working? Website: String startPage = "http://www.domiporta.pl/mieszkanie/sprzedam?Localization=dolno%C5%9Bl%C4%85skie&PageNumber=24&SortingOrder=InsertionDate"; Code: List<WebEle ...

A guide to toggling the check/uncheck status of a list box by clicking on a checkbox using

I am using a div and CSS id to control the checkbox check and uncheck functionality, but I am not getting the desired outcome from my source code Step 1: By default, the checkbox is unchecked and the listbox is disabled Step 2: When the checkbox is check ...

Stop HTML audio playback upon clicking on a specific element

I'm looking to add background music to my website with a twist - a music video that pops up when the play button is clicked. But, I need help figuring out how to pause the background music once the user hits play. Play Button HTML - The play button t ...

Reading complex table structures with Selenium

Incorporating Selenium into my Java project, I am on a mission to purchase coupons during a test and subsequently display them in the coupon overview. Multiple coupon packages can appear in this area if the user has previously made purchases. The structur ...

Issue with form validation, code malfunctioning

I am struggling to figure out why this validation isn't working. Here is the HTML code I'm using: <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type='text/javascript' src="scripts. ...

The information is not appearing in the dropdown menu

The select tag for chapters is not displaying the result from the query properly. Instead of showing in the select tag, the chapter names are being displayed as echo statements. <!doctype html> <html> <head> <meta charset="utf-8"> ...

Is there a way to achieve a transparent background while animating the second text?

I am seeking to create a unique typography animation that involves animating the second text, which is colored and consists of multiple text elements to animate. The animation should showcase each text element appearing and disappearing one after the other ...

How to explicitly set a time out in Python with Selenium

I am facing an issue where my code stops executing after clicking the link below. monkey_click_by_id(driver, "fwupdatelink") Is there a foolproof way to ensure that the code continues executing after clicking the link? ...

Discovering the login details element with Selenium in Python is a simple task

Currently in the process of learning how to use Selenium with Python. I have been practicing on the BBC website, but I am a bit stuck on adding code for a specific screen. Specifically, I need help with identifying the fields "Email or username" and "Passw ...

Highlight the phrase "figure x" within the paragraph to emphasize its importance

I want to emphasize figure 1, figure2, ...all the way up to figure 111. I am limited to making changes in HTML and CSS only, without using JavaScript or jQuery. Is there a different method than just inserting <strong> in the HTML? In CSS, we have : ...

What is the reason for JavaScript consistently returning the initial value as the result?

My current issue involves customizing Joomla article content using a module. I am attempting to hide a div until a user clicks on an input, such as a radio button labeled Test1. Once Test1 is selected, another hidden field within the div should display the ...

Guide to swapping images based on conditions using Angular JS

I am trying to display an image based on data received from an API response instead of text. If the value is true, I want to show an access symbol. If the value is false, I want to show a deny symbol. However, when attempting this, I am only getting th ...

Tips on automatically populating a textbox without the need for a button click

I am currently using the following code: <input type="text" value="<?php echo empty($this->session->store['actual_info']['actual_total_marketing_budget']) ? '' : $this->session->store['actual_info' ...

Issues with changing background colors using Jquery animate

I am attempting to create a fading background color effect when a button is clicked. Currently, I can change the background color using this code: $("#" + lblqty).css("background","#e9f1ff"); However, when I try to use the animate function as shown below ...

Selenium: Issue with sending messages on WhatsApp through the path is not functioning

Yesterday, I attempted to create a code for sending messages via WhatsApp. However, no matter how many different approaches I tried to insert the message into the chat box, I kept encountering the same error message: "unknown error: call function result mi ...

Guide to Changing the Value of a Textbox Using Form jQuery

For instance: <form id="example1"> <input type="text" name="example_input"> </form> <form id="example2"> <input type="text" name="example_input"> </form> In the code above, both text boxes have the same name. H ...

Point is not clickable - utilizing SELENIUM with PYTHON

How can I expand the arrow as shown in the image below from https://i.stack.imgur.com/tFqGY.png This is the code snippet I am using: from bs4 import BeautifulSoup from selenium import webdriver from selenium.webdriver.chrome.options import Options from s ...

What impact does reducing the window size have on my header?

While working on a website with a navigation bar, I encountered an issue where the navbar was not responsive. When the window size was reduced, the nav bar shifted to an awkward position vertically, as shown in the first image. The navbar shifts below the ...

Bootstrap Modal closing problem

While working on a bootstrap modal, I encountered an issue where the modal contains two buttons - one for printing the content and another for closing the modal. Here is the code snippet for the modal in my aspx page: <div class="modal fade" id="myMod ...

Utilize the Bootstrap column push-pull feature on the mobile version of

https://i.stack.imgur.com/yTBIt.png When viewing the desktop version, div A is displayed at the top of the page. However, I would like to move it to the bottom when viewing on a mobile device (col-xs). I have attempted to solve this issue myself without s ...