How can I use the selenium to check if the “load more” button exists on not page or not?

264    Asked by DavidWHITE in Salesforce , Asked on May 9, 2024

 I am currently engaged in a particular task that is related to testing an e-commerce website that can display product reviews on a page. As a part of my testing, I need to verify if the “load more” button exists on the page. How can I use the selenium to check if the “Load more” button elements exist in the HTML DOM? 

Answered by Donna Chapman

 In the context of selenium, You can use selenium in Python programming language to check if an element exists or not on a particular web-based page. Here is how you can do it:-

From selenium import webdriver
From selenium.common.exceptions import NoSuchElementException
# Initialize the WebDriver (e.g., ChromeDriver)
Driver = webdriver.Chrome()
# Navigate to the webpage
Driver.get(https://example.com/product_reviews)
Try:
    # Check if the “Load More” button exists by finding its XPath
    Load_more_button = driver.find_element_by_xpath(“//button[contains(text(), ‘Load More’)]”)
    Print(“The ‘Load More’ button exists on the page.”)
Except NoSuchElementException:
    Print(“The ‘Load More’ button does not exist on the page.”)
# Close the WebDriver
Driver.quit()

Here is the example given by using java programming language along with selenium of how you can check if an element is existing on a web page or not:-

Import org.openqa.selenium.By;
Import org.openqa.selenium.NoSuchElementException;
Import org.openqa.selenium.WebDriver;
Import org.openqa.selenium.WebElement;
Import org.openqa.selenium.chrome.ChromeDriver;
Public class ElementExistenceCheck {
    Public static void main(String[] args) {
        // Set the path to the ChromeDriver executable
        System.setProperty(“webdriver.chrome.driver”, “path_to_chromedriver”);
        // Initialize the WebDriver
        WebDriver driver = new ChromeDriver();
        // Navigate to the webpage
        Driver.get(https://example.com/product_reviews);
        Try {
            // Check if the “Load More” button exists by finding it using XPath
            WebElement loadMoreButton = driver.findElement(By.xpath(“//button[contains(text(), ‘Load More’)]”));
            System.out.println(“The ‘Load More’ button exists on the page.”);
        } catch (NoSuchElementException e) {
            System.out.println(“The ‘Load More’ button does not exist on the page.”);
        }
        // Close the WebDriver
        Driver.quit();
    }
}

Here is the example given by using HTML of how you can check if an element is existing on a web page or not:-




    Element Existence Check



   

        Product Reviews

       


Your Answer