How to use Selenium's "get attribute" to extract a profile picture's "src"?

223    Asked by DominicPoole in QA Testing , Asked on Jun 21, 2024

I am currently engaged in a particular task that is related to testing a web-based application that can allow users to upload a profile picture. As a part of my automation testing by using selenium in Python, I need to verify that after a user uploads a profile picture, the image file format should remain unchanged. 

How can I use the “get attribute” method of selenium in Python for extracting the “src” attribute of the uploaded profile picture element and then check whether the file format matches the expected format or not? 

Answered by Elvera Peasley

In the context of selenium, you can achieve this particular objective by using these steps:-

Locating the profile picture element

First, you would need to locate the element that represents the profile picture on the web-based page by using the selenium fund element method.

Getting the value of the src attribute

Once you have the element you can use the “get attribute” method to retrieve the value of the “src” attribute which contains the URL of the uploaded page.

Extract the file format from the URL

Next, you should extract the file format from the URL by using the string manipulation method such as “split() or regular expression.

Compare with the expected format

Now you can compare the extracted file format with the expected file format for the purpose of ensuring that the profile picture file format should remain unchanged after being uploaded.

Here is the Python coding structure given:-

  • From selenium import webdriver
  • From selenium.webdriver.common.by import By
  • From selenium.webdriver.support.ui import WebDriverWait
  • From selenium.webdriver.support import expected_conditions as EC

# Initialize the WebDriver (assuming Chrome here)
Driver = webdriver.Chrome()
# Open the website
Driver.get(https://www.example.com)
Try:
    # Wait for the login button to be clickable
    Login_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, “//button[@id=’loginButton’]”))
    )
    # Perform actions on the button if needed
    Login_button.click()
    # Wait for the next page to load (assuming a login page)
    # You might need to adjust the wait time or conditions based on the actual application
    WebDriverWait(driver, 10).until(
        EC.title_contains(“Login Page”)
    )
    # Perform login actions here (enter username, password, etc.)
    # Wait for the confirmation or next page after login
    WebDriverWait(driver, 10).until(
        EC.title_contains(“Welcome Page”)
    )
    # Continue with further actions after successful login
Except Exception as e:
    # Handle any exceptions that may occur during the process
    Print(f”An error occurred: {e}”)
Finally:
    # Close the WebDriver
    Driver.quit()

Here Is the java coding structure given:-

Import org.openqa.selenium.By;
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 WebsiteLoginAutomation {
    Public static void main(String[] args) {
        // Set the path to the ChromeDriver executable
        System.setProperty(“webdriver.chrome.driver”, “/path/to/chromedriver”);
        // Initialize the ChromeDriver
        WebDriver driver = new ChromeDriver();
        // Open the website
        Driver.get(https://www.example.com);
        Try {
            // Wait for the login button to be clickable using WebDriverWait
            WebDriverWait wait = new WebDriverWait(driver, 10);
            WebElement loginButton = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(“//button[@id=’loginButton’]”)));
            // Perform click action on the login button
            loginButton.click();
            // Wait for the login page to load
            Wait.until(ExpectedConditions.titleContains(“Login Page”));
            // Perform login actions (enter username, password, etc.)
            WebElement usernameInput = driver.findElement(By.xpath(“//input[@id=’username’]”));
            usernameInput.sendKeys(“your_username”);
            WebElement passwordInput = driver.findElement(By.xpath(“//input[@id=’password’]”));
            passwordInput.sendKeys(“your_password”);
            // Locate and click the submit button
            WebElement submitButton = driver.findElement(By.xpath(“//button[@type=’submit’]”));
            submitButton.click();
            // Wait for the confirmation or next page after login
            Wait.until(ExpectedConditions.titleContains(“Welcome Page”));
            // Continue with further actions after successful login
            System.out.println(“Login successful. Continuing with further actions…”);
        } catch (Exception e) {
            // Handle any exceptions that may occur during the process
            System.err.println(“An error occurred: “ + e.getMessage());
        } finally {
            // Close the WebDriver
            Driver.quit();
        }
    }
}


Your Answer

Interviews

Parent Categories