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

Limit the size of images with CSS properties like max-height or max-width

What is the preferred method for restricting the size of an image and why? While max-width can be used to restrict the size of an element, this article recommends using it specifically for images within containers. If you have a web application with a ve ...

What is the reason for the jQuery plugin not being applied after replacing the page content with an Ajax response?

At the moment, I am utilizing jQuery ajax to dynamically add content to my website. Additionally, I have incorporated the jquery.selectbox-0.6.1.js plugin to enhance the style of select boxes on the page. The plugin successfully styles the select boxes up ...

The URL specified in the driver.get() function is not being navigated to as expected

When attempting to open the browser with the webdriver, it is not redirecting to the specified URL and instead showing an exception message: Traceback (most recent call last): File "C:/Users/91800/PycharmProjects/Automation/automation.py", li ...

Running selenium and behave on a local port: A step-by-step guide

Having successfully integrated Selenium and Behave to interact with external websites, I am now faced with the challenge of making them work together with my fullstack python application running on local port 8000. When attempting to run Selenium code to a ...

Achieving consistent image column heights that decrease evenly

I am trying to create a grid-like layout where each column in a row has the same height and contains an image while maintaining its proportions. Currently, I am using the flex-grow property to adjust the ratio of the images. Although this works, it often ...

When using Selenium async script in its own thread, it can interrupt the execution of other

Let's consider this situation: Various scripts need to run in the browser. One of them involves sending messages from one browser to another (WebRTC). I am interested in measuring the delay for each operation, especially when it comes to sending mess ...

Whenever I press a button, my desire is for each picture to seamlessly reposition itself in the exact same spot on the screen

Having some issues with my code. When I click a button, the plan is for the pictures to change one by one to simulate a traffic light sequence. However, when I run the code, all the pictures appear at once without any effect from the button. Any help in ac ...

Removing accordion borders in Bootstrap and AngularJS can be achieved by customizing

Can anyone help me figure out how to hide the border in each accordion group that contains a panel and inner elements? I've tried using classes like .accordion and .inner but nothing seems to work. Any advice on where to find the specific class or ID ...

What is the best way to navigate between various websites using selenium and python?

I've almost completed my script that automatically generates a new PayPal account. However, I'd like to enhance the script by pulling information from another website before closing the driver. Specifically, I want the script to navigate to Gmai ...

Center the radio buttons regardless of the length of the text

My dilemma lies with 3 radio buttons and their corresponding labels - while 2 of them are aligned perfectly beneath each other due to their matching character lengths, the middle one extends further making it look misaligned. Check out this fiddle. Below ...

My toggleclass function seems to be malfunctioning

I am encountering a strange issue with my jQuery script. It seems to work initially when I toggle between classes, but then requires an extra click every time I want to repeat the process. The classes switch as expected at first, but subsequent toggles req ...

Scalable vector graphics require adjustments in scaling and translation, with variations between Chrome and Firefox browsers

I have an SVG diagram with some yellow points represented as circles. <html> <title>Yellow circles</title> <body> <svg version="1.1" id="Слой_1" xmlns="http://www.w3.org/2000/svg" xlink="http://www.w3.org/1999/xlink" ...

Looking to adjust the positioning of text being inserted into a column - unable to utilize text-align or float properties

I'm struggling to position my text in a specific area within a column using CSS. Our app displays a price when a quantity is selected, but I can't seem to center the $14 under 'Item Total'. Even using float: center; hasn't worked f ...

Ensure your mobile site is optimized for full width display

Imagine a webpage with a fixed width of 1280px. Is there a way to instruct a smartphone to automatically scale this page to full width upon loading? Currently, I am using: <meta name="viewport" content="width=1280, initial-scale=1"> However, it d ...

Attempting to use Selenium in JavaScript to download a file with a new element called 'TransitionRejection'

I am looking to streamline the process of downloading multiple files from a website. To achieve this, I navigate through several pages to obtain the file IDs using selenium and a perl script. Since selenium does not have a built-in download function and u ...

Launching WebDriver more efficiently with Selenium in Python

Does anyone know of a method to speed up the initialization time of the chromedriver in Selenium? Currently, it's taking approximately 6 seconds to start, which is causing a significant delay in my code execution. ...

There are no interactive or clickable elements on this page

Hey there, I'm facing some serious issues while trying to log into Google Adwords using ghostdriver for selenium. Here's a snippet of the code I am using: Dim driver As New PhantomJSDriver Dim options = New PhantomJSOptions() options.Add ...

Alteration of Content Height Input

I have designed a layout that I am excited to experiment with more in the future. Here is the current structure of my HTML: <!DOCTYPE html> <html> <head> <title>Title</title> <link rel="stylesheet" type="text/css" hre ...

Cells within a table are equivalent

I am attempting to ensure that all table cells match the size of the image, but for some reason, they are not aligning properly. Visit this link for reference. Here is a condensed version of the HTML code: <table> <tr> <td> ...

Issue with React MUI V5 causing theme not to display properly within shadow-root element

After trying to attach the shadow root using the MUI createTheme function, I encountered rendering issues. Despite following the instructions in this helpful guide on the MUI website, my problem persists. // App.js const cache = createCache({ key: ' ...