How can I solve the issue of the invisibility of a button on a particular webpage?

19    Asked by DipikaAgarwal in QA Testing , Asked on May 8, 2024

 I am currently engaged in a particular task that is related to automating a web testing scenario by using selenium in Python. The webpage contains a button that is initially not visible on the screen and thus it requires scrolling to become visible. How can I ensure that the selenium script school scrolls to the button before performing any action on it? 

Answered by Elvera Peasley

In the context of selenium, here is the example given of how you can use the selenium in Python for scrolling to a button before performing actions on it:-


From selenium import webdriver
From selenium.webdriver.common.by import By
From selenium.webdriver.common.keys import Keys
From selenium.webdriver.support.ui import WebDriverWait
From selenium.webdriver.support import expected_conditions as EC
# Initialize the Chrome WebDriver
Driver = webdriver.Chrome()

Try:

    # Navigate to the webpage
    Driver.get(https://www.example.com)
    # Wait for the button to be present on the page
    Button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, “//button[contains(text(),’Your Button Text’)]”)))
    # Scroll to the button using JavaScript
    Driver.execute_script(“arguments[0].scrollIntoView(true);”, button)
    # Click the button or perform any other actions
    Button.click()

Finally:

    # Close the WebDriver
    Driver.quit()

Here is the example given in java programming language of how you can scroll to a button before performing actions on it:-

Import org.openqa.selenium.By;
Import org.openqa.selenium.JavascriptExecutor;
Import org.openqa.selenium.WebDriver;
Import org.openqa.selenium.WebElement;
Import org.openqa.selenium.chrome.ChromeDriver;
Import org.openqa.selenium.support.ui.ExpectedConditions;
Import org.openqa.selenium.support.ui.WebDriverWait;
Public class ScrollToButtonExample {
    Public static void main(String[] args) {
        // Set the path to the Chrome WebDriver executable
        System.setProperty(“webdriver.chrome.driver”, “path_to_chromedriver.exe”); // Update with your Chrome WebDriver path
        // Initialize the Chrome WebDriver
        WebDriver driver = new ChromeDriver();
        Try {
            // Navigate to the webpage
            Driver.get(https://www.example.com);
            // Wait for the button to be present on the page
            WebDriverWait wait = new WebDriverWait(driver, 10);
            WebElement button = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(“//button[contains(text(),’Your Button Text’)]”)));
            // Scroll to the button using JavaScript
            ((JavascriptExecutor) driver).executeScript(“arguments[0].scrollIntoView(true);”, button);
            // Click the button or perform any other actions
            Button.click();
        } finally {
            // Close the WebDriver
            Driver.quit();
        }
    }
}

Here is the example given in HTML:-




<meta</span> charset=”UTF-8”>

<meta</span> name=”viewport” content=”width=device-width, initial-scale=1.0”>

Scroll to Button




 

   


Your Answer