Adam
Adam

Reputation: 19

How to verify whether a certain word is present in the loaded page through WebDriver?

How can I verify if a user sees on the screen a word, for instance “book” or “Book” ( on the webpage like https://www.amazon.com/gp/gw/ajax/s.html?ie=UTF8&ref_=nav_logo ? A script should check everything what a user sees including images (“title” and “alt”). The script should return True or False. How can I check the whole page using assert True, assert False ? Has anybody written something like this in Python ?

Upvotes: 1

Views: 992

Answers (2)

Yu Zhang
Yu Zhang

Reputation: 2149

Yeah, I have written similar tests to make sure a page is properly loaded.

  1. First of all, you need to get hold of those elements' identities from HTML, in order to achieve this, you can use Firefox plugin: Firebug or Chrome's developer mode to inspect elements.
  2. Once you have got hold of your element's identity, you have a few options from here, you can either locate an element by Xpath or locate an element by Css selector, please go to w3school for more detailed tutorials.
  3. When an element is located, using either find_element_by_xpath or find_element_by_css_selector, you can use element.innerText() function to get their texts, I think they are what you are after.
  4. You can look into the entire HTML page, verifying if those texts you expect will appear.

Hope it can help you out a bit.

Upvotes: 0

sujith
sujith

Reputation: 675

Get the complete text of the page .

WebDriver driver = new FirefoxDriver();
String bodyText = driver.findElement(By.tagName("body")).getText();
//if the word you would like to check is "Book"
boolean isWordPresent = bodyText.contains("Book");

Maintain a list of the words you wish to verify on the page and loop through the list.

Upvotes: 1

Related Questions