Tips on scrolling the web page until the desired web element is visible and carrying out necessary tasks

When searching for input, multiple table data is retrieved. A "show as text link" is present in different table rows that needs to be clicked.

I wrote a code that works fine in IE on one machine but does not work on a different machine due to variations in monitor size!

       String rawTextXpath = "//table[@id='view518-table']/tbody/tr[1]/td[4]/div[1]/a[text()='Show as raw text']";
            boolean exists = false;
            exists = driver.findElements(By.xpath(rawTextXpath)).size() != 0;

            if (exists == false) {
                system.out.println("No Results found!");
            }
            int i = 1;
            while (exists == true) { 
                try {

                    driver.findElement(By.xpath(rawTextXpath)).click();
                    Thread.sleep(3000);



                }       

                catch (Exception e) {

                }   

                String expandXpath = "//table[@id='view518-table']/tbody/tr[" + i + "]/td[1]/a";
                driver.findElement(By.xpath(expandXpath)).click();

                JavascriptExecutor jse = (JavascriptExecutor) driver;
                jse.executeScript("window.scrollBy(0,150)", "");



                rawTextXpath = "//table[@id='view518-table']/tbody/tr[" + (i + 1)
                        + "]/td[4]/div[1]/a[text()='Show as raw text']";

                exists = driver.findElements(By.xpath(rawTextXpath)).size() != 0;

                i++; 

            }

Note: In the script above, I scroll the webpage by 150 units which works fine in IE on one machine, but not in the same browser on a different system.

Steps: 1. Click on the show as text link in each table row and then click on the expand icon

The above code does not function properly in a different system using the same IE11 browser because of varying monitor sizes!

Answer №1

Give this a shot

WebElement element = driver.findElement(By.id("id_of_element"));

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

Thread.sleep(500);

Answer №2

To adjust for the window's height, remember to deduct it by using the animate jquery library in order to trigger a different function when scrolling downwards.

$("html, body").animate({ scrollTop: $(document).height()-$(window).height() });

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

When I attempt to translate R.string in FragmentPagerAdapter, an unusual number suddenly pops up before me

Struggling to insert a text translation into a FragmentPagerAdapter, I've tried various methods like using return (R.string.Chats) + ""; An unusual occurrence for me is the appearance of a strange number in three tabs. Image: https://i.sst ...

AngularJS allows for the creation of 2D arrays using the $Index

Currently, I am working on a project using AngularJS that involves creating a spreadsheet from a 2D array generated by the ng-repeat function. As part of this project, I am developing a function to update the initial values of the array when users input ne ...

decide whether to use webpack bundling during development or not

Whenever I save a file, it takes around 30 seconds for the changes to reflect. I am currently using gulp watch and webpack for bundling around a hundred files. Is there any way to speed up the build process? ...

Something is not quite right when the page is loading in a Ruby on Rails application

In the process of developing a wiki clone, I am faced with an issue involving JavaScript. When I navigate to a specific page by clicking on a link, the JavaScript does not function properly until I refresh the page. The JavaScript aspect mainly involves an ...

Nuxt Js - Ensuring script is only loaded once during the initial page load

I already have a static website design, but now I'm converting it to Nuxt.js to make it more interactive. After running my Nuxt server with "npm run build...npm run start," the scripts load and my carousel/slides work fine. However, when I navigate to ...

Python Selenium helps to separate locators from page object classes

I am currently working on implementing the POM (Page Object Model) in my automated tests, but I have encountered an issue with separating locators into a different file. I came across this helpful guide that I've been using as a reference: https://sel ...

Learn how to pass a JavaScript variable value to a PHP variable effectively

I have created a JavaScript variable and set it to a value. <script> var points = 25; </script> Now, I need to access this value in PHP for some specific reason but I am unsure how to accomplish that. ...

Creating a dynamic background color that pulses with animation

I recently created a script to animate the menu li element when hovering over the corresponding a element. Everything is functioning as expected, but now I'm looking to enhance the effect by having it continue as long as the mouse remains over the a e ...

Having trouble with radio button validation in JS?

I'm having difficulty understanding why the radio button is not staying checked when I select another option. To demonstrate, I created a fiddle where initially the "diy" button is selected but switching to "paid" causes the radio button to jump back ...

Having trouble updating a MongoDB array through a RESTful API PUT request

I'm brand new to the MEAN stack and recently completed a tutorial. I've been working on enhancing a simple application that utilizes a RESTful API to update a MongoDB. Currently, I'm using a PUT statement to modify specific parts of my colle ...

Is it advisable to run this function asynchronously on the server?

I have limited experience with node js, but I am working on a project similar to how Uber shows their cars in real time on a map. Currently, I have an SQL database containing data for numerous cars along with their GPS locations. The client sends their GP ...

Having trouble understanding why my submission button isn't working

I'm struggling to understand why my submit button isn't working correctly. Initially, I had an e.preventDefault() at the beginning and it didn't have any effect. However, after receiving advice from an instructor, I included it in the condit ...

The buttons on the Bootstrap Carousel are unresponsive and the indicators are not displaying as they should

I am currently attempting to replicate a bootstrap carousel that includes indicators. However, in my code, I am unable to view the indicators, and the previous/next buttons are not functional. Although I came across a workaround that I could modify from th ...

The function getattribute() on the edge is malfunctioning and returning a null value

Has anyone encountered issues with the getAttribute() function while creating an extension for Edge? I am currently facing a problem where it is not returning the attributes of the element that I am searching for. This is what my code looks like on Edge a ...

Tips to avoid conflicts between endpoints when building a REST API in an Express application

I am currently working with a REST API in an Express application to retrieve orders. http://localhost:2000/api/orders/chemists?orderBy=date&startDate=date1&endDate=date2 http://localhost:2000/api/orders/chemists?orderBy=chemist&startDate=date ...

I would like to create a map showing the selected locations by checking them off using checkboxes

How can I draw a road map by selecting locations with checkboxes in form.php? After posting the selected locations to map.php file and assigning them to a $location array, how do I then assign these values to the waypoints array in the javascript calcRoute ...

Generating exclusion filter based on user input

I am attempting to create a search string that can identify a specific character during the search process. I want to utilize it as an exclusion marker if the text includes the symbol -. The exclusion should only apply when the - character is preceded by ...

Exploring jQuery's AJAX capabilities in handling cross-domain requests and implementing Basic Authentication

I attempted the solutions provided in the suggested duplicate question, but unfortunately, they did not yield the desired outcome. I have been struggling to utilize AJAX to POST data to a remote server's API from a local PC client (testing on Chrome ...

Facing challenges when running asynchronous mocha tests using async/await syntax

Working on E2E tests using mocha and selenium-webdriver has been quite a challenge for me. Although I have implemented async/await functions to handle most of the tests smoothly, I am facing an issue where none of the tests are completing successfully. Her ...

Subclassing Observable in RxJS 5: Parent class instances are returned by static methods

I've been experimenting with subclassing the RxJS Observable class and overriding the lift method, as explained here. While I can successfully add new operators to the prototype, when trying to create a new Observable using my subclass (such as MyObs ...