The pageSource in IE Webdriver fails to refresh after a change on the page caused by JavaScript

Currently, I am working on automating a Web application using Selenium in C#. My task involves clicking on a link on the home page which redirects me to another page. To switch to this new page, I have implemented the following code:

string parent = webDriver.CurrentWindowHandle;
while (webDriver.WindowHandles.Count <= 1) ; // waiting for new tab
foreach (string handle in webDriver.WindowHandles)
{
    if (handle != parent)
    {
        webDriver.SwitchTo().Window(handle);
        break;
    }
}

This new page contains only two links that allow users to select their role. Upon clicking the second link, a JavaScript function modifies the entire page and loads new data on the same page. However, even after the page has changed, the webdriver continues to return the same old page source with just the initial two links. The title of the changed page is updated correctly by the browser, but the webdriver does not provide the latest page source as expected.

After reading the documentation, it seems that IE webdriver does not always return the most up-to-date page source. Given this limitation, I conducted a small test using the

webdriver.FindElements(By.XPath(//a);
method to fetch tags from the new page. Unfortunately, instead of retrieving tags from the modified page, the driver returned tags from the original page containing the selection links. This has left me puzzled and unsure why the driver is failing to deliver the latest tags from the updated page.

I am stuck on this issue and would sincerely appreciate any guidance or assistance. Thank you in advance!

Answer №1

It is crucial to make sure that the page has fully loaded before retrieving the page source:

WebDriverWait wait = new WebDriverWait(driver, 20);

// transitioning to the next window
String main_handle = driver.getWindowHandle();
wait.until((WebDriver drv) -> {
    for (String handle : drv.getWindowHandles()) {
        if (handle != main_handle) {
            drv.switchTo().window(handle);
            return true;
        }
    }
    return false;
});

// waiting for an element that signifies the page has finished loading
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("...")));

// obtaining the page source
String page_source = driver.getPageSource();

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

Choose a region on a scatter plot using JavaScript

My current challenge involves utilizing JavaScript to achieve the following tasks: Create a scatter plot consisting of approximately 10,000 data points Enable users to draw a curved shape on top of the plot in order to select a specific region (The desig ...

delivering the optimized main RequireJS file as a static asset through NGINX servers

Is it true that serving static assets from NGINX or another server is better than using your Node.js application server? In my case, I have a single-page application where in production mode, only one optimized .js file is served from the index page, and ...

How can I display random images in the gallery?

I am looking for a gallery that is similar to the one in this provided link: The codes seem too complex for me to figure out on my own. Thank you ...

Display the country code according to the option chosen by the user in the dropdown menu

To enhance user experience, I am implementing a feature where the user can select their country from a drop-down list and then have the corresponding country code displayed in a text field for easy entry of their phone number. Below is the list of countri ...

What is the best way to choose an item from the selection of options in the dropdown menu within

https://i.sstatic.net/dkKXn.png self.wait.until(EC.element_to_be_clickable((By.NAME, "job_sector"))).click() # job selector time.sleep(0.5) jobs = self.driver.find_elements_by_class_name("apz_typeahead__prediction_content&qu ...

When changing recipients in Firebase, the Javascript code fetches the same message multiple times even though there is only a single message stored in the database

In the process of developing a chat application using Firebase and JavaScript, I have come across an issue. Whenever I switch receivers multiple times, the message I send is fetched multiple times even though it is only sent once to the database. var selec ...

Tips for transferring information from a React form into a MySQL database?

Currently, I am venturing into developing my first react application. The backend functionality is operating smoothly; however, I am facing challenges with integrating the input data from a react form into my database. I attempted to use axios for this tas ...

Comparing jQuery AJAX with UpdatePanel

Our team is facing the challenge of dealing with a page containing an overwhelming amount of jQuery code (specifically around 2000 lines) that has become difficult to maintain. We are considering shifting some of this functionality to the server side for ...

Struggling to create an access token with the Slack API

My goal is to retrieve an access token from the Slack API. When I use the code provided below, it generates an authorization URL containing a temporary code in the query string. Once permissions are granted, the process redirects to /slack/oauthcallback. ...

Cannot display GIF file from the SRC directory in a React application

I am trying to display a gif from the "/src/images" folder in my component, but I keep getting my "old" value displayed instead. What could be causing this issue? Snippet from Danke.js site: import Confetti from "../images/confetti.gif"; <Box sx={{ ju ...

Attempt to locate an element using Selenium in Python and continually refresh the page until the element is found

My current script locates and clicks on an element, but there are instances when the element is not present due to the item not being released yet. I would like the script to verify if the item is available and if so, click on it. If not, the page should b ...

Is there a way to only update a single child item in an array without modifying the entire document?

This is the model I am working with: public class Student { [BsonId] [BsonRepresentation(BsonType.ObjectId)] public string Id { get; set; } public Prop[] ParentProp {get; set;} } public class Prop { [BsonId] [BsonRepresentation(BsonType.Obj ...

Discovering elements - Selenium (Python): How to populate with empty values (or alternative values) rather than leaving blank

Hey there, I'm currently working on a web scraping project and encountering an issue. I anticipated getting a list of 600 values upon running the project, but it only returns a list of 450 values. I understand that FindElements creates a list with t ...

Why am I seeing numbers in the output when I log the data from the res.write(data) function in Node.js?

While working with Node.js on my Raspberry Pi, I encountered an issue where reading a local file 'test.html' resulted in hex output instead of the expected HTML format. Can someone explain why this might be happening? Additionally, I am aware tha ...

Searching for items in a JSON object using binary search

My JSON object contains data organized like this: "time1": { "item1": "val1", "item2": "val2" }, "time2": { "item1": "val3", "item2": "val4" } ... The time* values represent dates in milliseconds and are sorted in sequence. Now, I need t ...

DetailsView FileUpload does not detect file presence because HasFile is always false

At the forefront, I want to clarify that I am not utilizing an UpdatePanel – seems like a common issue, but it was unfamiliar territory until I researched this problem. The challenge I'm facing involves a DetailsView that is supposed to upload a fi ...

Unable to locate the Chrome binary while running a Python test in Jenkins

In my current project, I am utilizing Python and Selenium to run a test form for my application. The code is developed in Eclipse Neon and functions perfectly when executed from Eclipse. However, when attempting to run the same code using Jenkins, an error ...

Opt for utilizing variables rather than string values containing relative paths in a TypeScript project

Is it possible to implement a namespace-like structure within a large project for importing modules? For instance, instead of import {formatNumber} from '../../../utils/formatters' Could I use import {formatNumber} from utils.formatters I b ...

A comprehensive guide on associating a JavaScript function with an element attribute

I am looking for a way to assign a JavaScript function to an HTML attribute. For example: <li data-ng-repeat="job in jobList" class= dynamicClass(str) data-filter = "dynamicFilter(str)"> The reason I want to do this is because the class name and ...

Symfony using Vite with Vue 3 encounters an error: Uncaught ReferenceError - exports is undefined

Currently, I am in the process of developing a Symfony 3 Application with Vite and Vue3 integrated with TypeScript. To replace Symfony's Webpack Encore, I opted for the Vite Buildtool using this convenient plugin: https://github.com/lhapaipai/vite-bu ...