Tips for verifying if JavaScript has modified HTML after receiving an AJAX response

We are experimenting with scraping a website using Selenium and Java. For example, we want to change the product color on this page. How can we determine when the website's JavaScript has altered the HTML after an AJAX response? And how long should we wait after the AJAX/XHR response for the HTML to be updated by the JavaScript?

It can be challenging to extract data from complex AJAX queries...

Here is the link for the JSON response: JSON Response

Answer №1

Learn how to utilize

org.openqa.selenium.support.ui.ExpectedConditions
and
org.openqa.selenium.support.ui.WebDriverWait
effectively by visiting this informative link

Check out the code below:

package tests;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class IgorSavinking {

    public static String userDir = System.getProperty("user.dir");
    public static String chromedriverPath = userDir + "\\resources\\chromedriver.exe";  

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", chromedriverPath);
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--ignore-certificate-errors");
        options.addArguments("--start-maximized");
        options.addArguments("--disable-notifications");
        WebDriver driver = new ChromeDriver(options);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        
        driver.get("https://www.ergodyne.com/shax-6054-pop-up-tent-sidewall-kit-includes-10ftx10ft");
        driver.findElement(By.id("cookie-policy-accept")).click();
        String colorBefore = driver.findElement(By.tagName("legend")).getText().substring(5);
        System.out.println("Current color is: " + colorBefore);
        driver.findElement(By.xpath("//label[contains(@for,'edit-purchased-entity-0-attributes-attribute-color-17')]")).click();
        new WebDriverWait(driver, 30).until(ExpectedConditions.numberOfElementsToBe(By.xpath("div[@class='message']"), 0));
        String colorAfter = driver.findElement(By.tagName("legend")).getText().substring(5);
        System.out.println("Current color is: " + colorAfter);
        System.out.println("Color has been changed: " + !colorBefore.equals(colorAfter));
        driver.quit(); 
    }

}

See the output generated from running the above code:

Starting ChromeDriver 97.0.4692.71 (adefa7837d02a07a604c1e6eff0b3a09422ab88d-refs/branch-heads/4692@{#1247}) on port 38237
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Úno 02, 2022 8:02:15 DOP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Current color is: Lime
Current color is: Blue
Color has been changed: true

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

Display different images based on user selection in vue.js

I am new to working with vue.js and I'm facing a challenge. My goal is to display images of NBA players based on the selected criteria using vue.js. For instance, if the Dunk contest champion is chosen, only images of Kobe and Jordan should be display ...

Error: Attempting to destructure without initializing / identifier 'name' is already in use [mysql][nodejs]

Whenever I attempt to pass keys as parameters, I'm encountering these errors in nodemon: SyntaxError: Identifier 'name' has already been declared This is the model function causing the issue: const create = async (name, about, site) => { ...

What could be causing the misalignment between the desired output and the response from the AJAX request

Below is a basic JavaScript file I am working with: $.ajax({ url: "is_complete.php", type: "post", success: function (data) { if(data == 1) { } alert("ok") } }) The message "ok" will only be di ...

What is the best method for sending a JavaScript variable to the server and back again?

I'm currently working on a JavaScript project where I need to build a string. Let's say, for the sake of this example: var cereal = 'my awesome string'; There's also a button on my webpage: <button id="send" type="submit" nam ...

Ensure the form is properly validated before initiating the submission process on 2checkout

Attempting to prevent the form from being submitted, I implemented the code below. Typically, this method works perfectly fine. However, when integrating 2checkout js (), it does not function as intended. <form onSubmit="validate(); return false;" meth ...

Working with Json serialization and deserialization in Scala 2.10

It seems that Scala 2.10 has caused compatibility issues with some old libraries like Jerkson and lift-json. The goal is to achieve the following level of usability: case class Person(name: String, height: String, attributes: Map[String, String], friends ...

What are the steps to view my HTML webpage on a smartphone?

Recently, I successfully created an HTML webpage with CSS and JS that looks and functions perfectly on my PC. However, when attempting to access it on my phone, I encountered some issues. Despite transferring all the necessary files to my phone, only the ...

A guide to incorporating Material-UI ThemeProvider and WithStyles with Typescript

I've been feeling frustrated lately as I've been dedicating the past few days to migrating my React application from JavaScript to TSX. While I appreciate the type checking that TSX provides, I'm struggling with understanding how to implemen ...

Web Scraping - Is there anyone who can help tidy this up?

I've been grappling with this web scraping project for days now, and as a newbie in this field, I find myself stuck on a crucial issue that I can't seem to resolve. My Challenges The problem lies in the span loop location - currently it prints ...

Expand the video comparison slider to occupy the entire width and height

I am striving to develop a video comparison slider that fills the height and width of the viewport, inspired by the techniques discussed in this informative article: Article Despite my efforts, I have not been successful in achieving this effect so far a ...

Combine arrays of JSON data within a JSON object using JavaScript

Looking for help with reformatting a JSON response {"result":[["abc","de"],["fgh"],["ij","kl"]]} Interested in transforming the response to: {"result":["abc","de","fgh","ij","kl"]} What's the best way to accomplish this task? ...

Why is the image cut in half on mobile devices but displaying correctly on computer screens? Could it be an issue with

There seems to be an issue on mobile screens that does not occur on computer screens. When the user clicks on the image, it disappears, and when they click another button, it reappears. However, there is a problem with how the image appears - it is cut off ...

What is the best way to open multiple browsers at the same time in Selenium without the need for running tests? I have noticed that some people use tests for this purpose, but

Is it possible to run multiple browsers at the same time in Selenium without relying on tests? I have come across methods that involve tests, but they are not suited to my requirements. My goal is to place each browser run within a separate method and ex ...

The color of the background does not remain changed permanently

I'm having an issue where the background color changes when I click a button, but reverts back almost immediately to its original color. How can I ensure that the new color stays permanently? Here is a simple JavaScript function you can use: functio ...

Instructions for adding username/password authentication using Express ntlm

Attempting to set up username and password authentication using Express-ntlm. I've included the following code as middleware: app.use( ntlm({ domain: '<domainname>', domaincontroller: '<ldap url>', })); I haven't ...

What is the most efficient way to update a counter when a button is clicked in React and display the result on a different page?

Just delving into the world of React and Javascript, I decided to challenge myself by creating a Magic 8 Ball application. Currently, I have set up two main pages: The magic 8 ball game page A stats page to showcase information about the magic 8 ball qu ...

Data retrieval from client-side fetch request results in a corrupted file upon download

I'm facing an issue while attempting to fetch a file using a GET request and download it directly in the browser. However, after the download is complete and I try to open the file, it seems to be corrupted. Strangely, only .txt files are opening corr ...

Create a counter using the data retrieved from the Axios fetch response

I have a task to fetch data from a back-end using axios and create a counter based on the number of completed tasks. The information needed for the counter is included in the response data from the back-end. However, I am facing difficulties in increment ...

Error: Module 'mongoose' not found

I have successfully created a basic CRUD API using nodeJS, express, and mongoDB. Everything was working smoothly until I decided to enhance the security by storing password hashes instead of plain text passwords. To achieve this, I integrated the bcrypt np ...

Is there a way to incorporate the load page plugin specifically for JavaScript while ensuring that my PHP code is still functional?

Is there a way to implement the loading page plugin "PACE" for PHP code execution instead of just JavaScript? I am more comfortable with PHP, CSS, and HTML, but not very experienced in JavaScript/AJAX. The PACE plugin is designed to show a progress bar fo ...