Use Javascript to set the position attribute to static for any styles that currently have the position attribute set to fixed

While web scraping using Selenium WebDriver with Chrome, I stumbled upon a page containing fixed regions that do not scroll and remain in place relative to the window. Often when trying to scroll to a specific control using Actions.MoveToElement(), the element ends up hidden behind one of these fixed regions. As a result, clicking on the obscured element triggers the fixed region instead, preventing my intended control from being clicked.

The fixed regions are identified by their class attribute class=SomeFixedPositionStyle. To overcome this issue, I am considering having Selenium inject Javascript code to iterate through each style on the page and change any instances of position:fixed to position:static. How can this be implemented?

I have decided against modifying the class attribute of the fixed elements as scrolling resets the class attribute to its original value, including the fixed positioning style.

For instance, you can observe this behavior on a page like where fixed bands appear at the top and bottom while scrolling.

When attempting to scroll to element //*[@id="holdingsTabs"]/ul/li[3] (the "All" link in the "Holdings" section), it gets positioned beneath the lower fixed region, making it inaccessible for clicking.

Answer №1

One way to apply a custom style to a webpage is by injecting a style rule directly into the page

var sheet = document.styleSheets[0];
sheet.insertRule("SomeFixedPositionStyle { position: static!important; }", 1);

Answer №2

In these types of scenarios, one effective technique is to bring an element into view by scrolling:

driver.executeScript("arguments[0].scrollIntoView();", element);

To eliminate the "stickiness," you can dynamically adjust the position of the wrapper element:

var wrapper = driver.findElement(webdriver.By.css('.sticky-wrapper'));
driver.executeScript("arguments[0].style.position = null;", wrapper);

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

Calculating the difference between the old scrolltop and the new scrolltop in jQuery/Javascript

I have a seemingly straightforward question, yet I am struggling to find a solution. Each time a user scrolls, the scrollTop value changes. I want to subtract the previous value from the new value of the scrollTop. However, I am unsure how to store the old ...

Efficient PHP login process enhanced by jQuery

After a user logs out on my website, they are redirected to the home page. However, if they navigate back in their browser, it still shows them as logged in (although logged in features don't work). I am considering implementing a solution where I us ...

Issue with loading Angular CSS files arises when managing two partials using a single controller

I am currently developing a web application using the MEAN stack. My project utilizes Bootstrap as the CSS library, with some CSS overrides present in two of my own files named "app.css" and "mio.css". Everything was running smoothly until I encountered an ...

Getting your routing to function properly in React.js involves setting up routes for different

I have been working on a React project, which can be found here, and trying to tackle the issue of relative links in the navbar. However, every time I click on About Me, I encounter this message: Cannot GET /about The chunk of code I am currently dealing ...

What is the best way to restrict the creation of objects in a JavaScript list?

Experiencing a fun challenge with HTML coding! Take a look at my ordered list in HTML: <ol id="myList"> <li>Tea</li> <li>Milk</li> <li>Water</li> </ol> <button onclick="myFunction()">Try it</ ...

Navigate to a specific section in an Accordion with the help of jQuery

I currently have 2 pages: page1.html, which contains a series of links, and page2.html, where an Accordion is located. The Query: I'm wondering if it's feasible to link directly to a specific section within the Accordion. For instance, if I want ...

"Exploring the ThreeJS library's ability to rotate objects within

I have a collection of individual objects in an Array that I want to rotate as a single object. Here is an example: //retrieve body parts from the console bodyParts //Result [THREE.Mesh, THREE.Mesh, THREE.Mesh, THREE.Mesh, THREE.Mesh, THREE.Mesh, THREE.M ...

Using Xpath in selenium without specifying the exact element can make

I am currently learning how to use Selenium Webdriver. I have been relying on Firebug and Firepath to create xpaths for identifying web elements, but I have encountered issues with using these xpaths (such as "Xpath cannot be evaluated into a web element") ...

Maximizing efficiency by utilizing factory or services to transfer data within AngularJS controllers

I am new to AngularJS and I am currently trying to understand how to push an array of objects data (not input strings) between controllers. Right now, my code is pushing the data into one controller ('ChooseTabCtrl') but I actually want it to go ...

Adjust the navigation height to be proportional to the main content size

I've been attempting to make my navigation menu take up the entire page, but I have yet to achieve success: My goal is for the green menu section to extend down to the footer. Here is the HTML code: <div ...

Flashing background color appears as I scroll

Whenever a user touchstarts on a div, I want to apply a background-color to that specific div. Then, as the user scrolls, I use the $(window).scroll event to either reset or remove the background-color from all divs. However, my issue arises when a user b ...

Generating a list of items to buy using a JSON document

So here's the json file I'm working with: [ {"task":"buy bread","who":"Sarah","dueDate":"2023-10-18","done":false}, {"task":"clean car","who":"David","dueDate":"2023-08-30","done":true}, {"task":"write report","who":"Jenny","dueDate":"2023-09 ...

Sending information to a personalized mat-grid element using properties

I am currently working on enhancing the functionality of the angular material mat-tree component by building a custom component. The main objective is to create a table with expandable and collapsible rows in a hierarchical structure. I have managed to ach ...

I'm wondering why myDivId.toggle() is functioning properly but myDivClass.toggle() is not working as expected

Using JQuery's toggle() function, I have been able to hide and show some DIVs successfully. Recently, I discovered relationships between certain DIVs that allowed me to group them into a class. I decided to try toggling the entire class rather than ...

If I type too quickly in the input field, it ends up displaying the incorrect div

I need to display two different dropdowns in my input field. To achieve this, I am using a method called shouldShowWarningBox that gets triggered every time the user updates the input and updates the value of showWarningBox accordingly. However, when I typ ...

What could be causing the WebdriverIO browser object to unexpectedly exit my loops?

Each time I try to invoke a method using WebDriverIO's browser object, it abruptly escapes from the current loop I'm in. Upon inspection with the debugger, it is evident that no other code within the loop is being executed. Despite the fact that ...

Using jQuery to Toggle the Height of Multiple Sections with a Single Function

I've got three different sections, each with varying heights and a simple structure like this: <section> <h2>My heading</h2> </section> What I want is for these sections to display at first, but then shrink dow ...

The webpack vue-loader throws an error message stating "unexpected token {" when processing a single-page .vue component

As a C# backend developer, I am venturing into the world of Vue.js. I typically work with Visual Studio 2017 + ASP.NET MVC (using it as an API + one layout) + Vue.js + Webpack. .vue single-page component files are loaded by vue-loader, while .js files a ...

Troubles encountered when cascading val(), text(), and data()

Here is my JavaScript/jQuery code: $select.append($("<option />") .val(this.id) .text(this.text) .data('name', this.name) .data('isstorage', this.isstorage)); Although it successfully assigns values to t ...

Extract the URL from an AJAX request within an IFRAME using a Chrome Extension

Currently, I am facing the following issue: I am trying to develop a Chrome extension, but encountering some major obstacles: The page I am working with loads an iframe. This iframe contains JavaScript code that triggers an AJAX request which returns an ...