Selenium Webdriver faced challenges in identifying dynamically created scripts using CSS selectors

I am in need of assistance with the following:

Requirement: On the homepage, there is a navigation menu bar that changes appearance when scrolling down. This change is reflected in the body class name switching from "home" to "home scriptable persistent-on". I need to validate this in my selenium Webdriver Test scripts.

Problem Statement: The issue arises when trying to use CSS selectors to identify the element by its class name. Due to the dynamic nature of the class name change and the fact that the page source only shows "body=home" even after scrolling, the identification process fails. Here is a snippet of the code where I have attempted different CSS selectors without success. Any guidance on how to handle this would be greatly appreciated, as I am stuck and need to resolve this quickly.

<body class="home scriptable persistent-on">
    <script>
    //<![CDATA[
            /* UI NOTE: - the page is designed by default to function with scripts turned off (for accessibility reasons) adding the class of "scriptable" dynamically allows us to target CSS to users with JavaScript enabled - this is the ONLY piece of JavaScript that should be executed at the top of the page */
            document.body.className += " scriptable";
        //]]>
          
 ...
          
</body>
 
 I'm using the following code
 
 private static final By BODY_FOR_STICKY_NAV = By.cssSelector("body.home.scriptable.persistent-on");
 driver.findElement(BODY_FOR_STICKY_NAV)
 
 I've tried using different ways in CSS selectors..nothing works out
      
      

enter code here

Answer №1

Make sure your findElement() function waits for the page to finish scrolling before executing. This can be done by waiting for an expected condition to occur.

// Use this code to scroll the page to the desired position and trigger the class change

// Then wait for the element to obtain the class
Wait<WebDriver> wait= new FluentWait<WebDriver>(driver).withTimeout(15L, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.css(".home.scriptable.persistent-on")));

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

What is the best way to incorporate a <li> view cap within a div element using JavaScript?

Currently, I am attempting to implement a dynamic <li> view limit within the content of a div. My goal is to display only 3 <li> elements each time the div content is scrolled. Although not confirmed, I believe this example may be helpful: ...

How can I ensure the carousel stays centered on a webpage even after resizing the browser window?

Currently in the process of developing a website, I have implemented a jquery-based carousel on the homepage sourced from here. However, substantial modifications were made to tailor its appearance specifically for the site. The issue at hand is that I hav ...

Label text will not be wrapped in front of the label (bootstrap)

I'm struggling with some css coding. I want the text to wrap so the label is positioned on the middle right of the box, similar to this example: . However, currently it looks like this: http://jsfiddle.net/yuvemL1z/ and the label ends up being pushed ...

Designing a user-friendly graphical interface (GUI) for the

I am interested in developing a user interface for this script that can automatically download PDF files. However, I am uncertain about where to begin. There are two specific input fields that require modification. drp.select_by_visible_text('**Dan Pi ...

Creating equality in height among input file fields and other elements with Bootstrap 5

Utilizing a straightforward template to display a registration form with a file attachment field: /* facing an issue when adjusting the height for myself */ .mycustom { line-height: 2.75; z-index: 999; } <!-- <a href="/cdn-cgi/l/email-protecti ...

Issue with image slider loop functionality not functioning properly

Looking to incorporate this image slider into my website: http://codepen.io/rslglover/pen/DBvoA The slider functions properly, but it halts after completing its cycle. I'm unsure of what distinguishes the CodePen code from mine. How can I replicate t ...

Need help aligning content with flexbox? Having trouble with align-content and justify-content not working correctly?

First and foremost, I want to express my gratitude in advance for any assistance you can provide. My goal was to neatly align three pieces of content within each row, similar to the image displayed here: https://i.sstatic.net/vtsRj.png To achieve this lay ...

Showing a notification that is persistent and not dismissible

Hello everyone! I've been struggling with a problem on the RPS project for quite some time now. The issue arises when I try to display a message for a tie in the game, but I can't seem to find a solution to make it disappear when the round is not ...

Selenium and Python are unable to locate the button within the script, whether it be using xpath, id,

I am a Python beginner, and I am working on a script that seems quite simple. I have successfully logged into the website, but I am facing an issue while trying to click on the "Market Express" button. Although I can identify the xpath (//[@id="MarketExpr ...

Utilizing Scenario Outlines in Specflow with Selenium using C#

I have a challenge with using a scenario outline in Specflow to generate tests based on the examples I provide, which I then need to execute using selenium and C#. The feature I've created looks like this: Scenario Outline: Login Given I have navigat ...

Animate a div to sense whether it has reached the top or bottom position

Is it possible for a div to animate when it reaches almost halfway while scrolling? I'm looking to achieve this effect on a sticky sidebar, similar to how it works on this website: sample This is the code I have: $(function(){ // document ready ...

Scraping web content using Selenium's iframe functionality

I have a need to extract toner values from the web pages of different printers in my office. The challenge I'm facing is that the page is structured with multiple frames, and the one containing the toner information is written in JavaScript which mak ...

Encountering a connection reset error with Urllib3 and Selenium, caused by WinError 10054 while trying to initialize

A new post is being made since the previous one was mistakenly marked as a duplicate, which is not the case. I'm encountering some difficulties with urllib3 and Selenium. Oddly enough, one of my selenium scripts (the others are functioning properly) ...

In what way could the border exceed the dimensions of the div?

<head> <meta charset="UTF-8"> <title>Document</title> <style> #Tri{ width:0px; height:0px; border-top: 50px solid yellow; border-right:50px solid red; border-bottom:50px solid green; border-left:50px solid blue; } </style&g ...

IE9 fails to display li::before element correctly

I have created a utility navigation bar using an unordered list and I am attempting to use li::before to add a pipe "|" symbol between each link in the navigation. Below is my utility navigation... <div class="horizontalnavlinks"> <ul> ...

Ways to modify the jquery script to display on page load rather than on scrolling action

Currently in the process of working on my site, www.runebs.dk. Utilizing a script to activate information for each project. JS.. $('.showHide').on('click',function(){ $(this).toggleClass('active'); $( '#subHead ...

Bespoke animated angular material tabs

Having an issue with Angular Material tabs. I have managed to remove the "slide" effect, but I can't figure out how to modify or remove the black effect behind my tab. Can anyone offer some assistance? black effect 1 black effect 2 Here is a snippe ...

Ways to retrieve the content within the inner span element

I'm trying to extract an integer value from a span element using Python Selenium. The HTML tag I'm referring to is shown below. Any assistance would be greatly appreciated! <span class="pendingCount"> <img src="/static/m ...

Centered perfectly in a bootstrap card

I'm struggling to create a bootstrap card with a logo perfectly centered, but the logo seems fixed at the top. How can I fix this issue? <div class="col-xl-3 col-md-6"> <div class="card mini-stat"> <div class="card-body tex ...

Creating a button definition in selenium without using the "find_element" method in Python

In my test automation project, I have a preference for pre-defining locators and assigning them to variables. This way, I can easily refer to the variable name later on in my code like so: login_button = <browser>.find_element_by_id("login") login_b ...