About this question
I have been tasked with writing automated test scripts by using the Selenium WebDriver to verify the presence and content of a Locating the Is the Verify that the text within the Log in to share your answer and help other learners.
Best Answer · By JanBask QA Expert
Answered on Jun 21, 2024 you can accomplish this particular task by using the selenium WebDriver in Python programming language by using the following coding snippet:- Here is how you can achieve this task by using the HTML:- Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it. Step-by-step QA Testing guides from industry experts Common QA Testing interview questions, answered Guides, tips and career advice on QA Testing from JanBask experts.Your answer
1 Answer
# Initialize the WebDriver (assuming Chrome here)
Driver = webdriver.Chrome()
# Open the website
Driver.get(https://www.example.com)
Try:
# Locate the element by its ID
Div_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, “div_id”))
)
# Check if the is displayed on the page
If div_element.is_displayed():
# Verify the text within the
Div_text = div_element.text
If div_text == “your account has been successfully updated”:
Print(“The text within the matches the expected text.”)
Else:
Print(“The text within the does not match the expected text.”)
Else:
Print(“The element is not displayed on the page.”)
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 how you can achieve this task by using the java programming language:-
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 VerifyDivText {
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 {
// Locate the element by its ID
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement divElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.id(“div_id”)));
// Check if the is displayed on the page
If (divElement.isDisplayed()) {
// Verify the text within the
String divText = divElement.getText();
If (“your account has been successfully updated”.equals(divText)) {
System.out.println(“The text within the matches the expected text.”);
} else {
System.out.println(“The text within the does not match the expected text.”);
}
} else {
System.out.println(“The element is not displayed on the page.”);
}
} 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();
}
}
}

More QA Testing discussions
Learn & Explore
QA Testing Tutorials
Browse all tutorials →
QA Testing Interview Q&A
Browse all interview Q&A →
Latest QA Testing Blogs
Selenium Selenium Tutorial: Complete Guide to Automation Testing for Beginners
The Selenium tutorial covers both fundamental & advanced Selenium concepts. It is for individuals at all levels. Learn about…
QA Testing What is Functional Testing? A Complete Tutorial Guide you need to know
What is Functional Testing? A Complete Functional Testing Tutorial Guide for Bignner - Functional Testing Types, Functional…
QA Testing Software Testing Basics: Fundamentals, Principles, Lifecycle & Best Practices
Learn software testing basics, including key principles, lifecycle stages, types of testing, and best practices. A complete…
QA Testing You Can’t Miss the Top 20 Security Testing Interview Questions
Are you getting ready for your information security interview? Get a hold of the top 20 information security interview questions…