1

I have been trying to get the text from various div and tables for some BDD's I am writing in Cucumber. Everytime I try to acquire anything from the frontend of the website I'm working on it just pulls nothing. I need to know what I specifically need to do to get this content.

The page element I'm trying to retrieve from is similar to this

 <div id="statMsg" class="statusMsg">Your changes are saved.</div>

These are the 2 methods that are currently trying to retrieve the text from the div.

 public WebElement savedText() {
    return webdriver.findElement(By.className("statusMsg"));

 public void UserClicksOnSave() throws InterruptedException {
    daSave.saveOn().click();
    daPage.savedText().getText();
Grasshopper
  • 8,908
  • 2
  • 17
  • 32
Jack Parker
  • 547
  • 2
  • 9
  • 32
  • Guess this is a timing issue, code is checking before the div is visible. Have u tried with an ExpectedCondition on visibility of this element. To test this, try placing a thread.sleep that shld confirm if it is a timing problem. Much better if u use id to select the element rather than class. Maybe u are getting some element in the hierarchy before the text element, with the same class attribute. – Grasshopper Dec 07 '18 at 08:23
  • I actually did this yesterday afternoon, and it worked. – Jack Parker Dec 07 '18 at 12:11
  • 1
    You're missing the point. There is no delay between the time your code clicks the save button and checks for the text. It would appear that you're using page objects, so it's also possible that you need to refresh the page object to get the text, but you might only need to wait until the page is finished before checking for the text. If it works sometimes but not others, it's almost always a timing issue, or a refresh issue. – Bill Hileman Dec 07 '18 at 15:37
  • I'm more versed in using Selenium with C#, but there should be a way to make the web driver "wait" for a certain condition to be true. Basically, make web driver wait for the text to appear in the DIV. It's likely you have a race condition going on between the button click and Selenium getting the text from the elements on screen where it is getting the text faster than the text is changed on screen. Or it's finding the DIV too fast. – Greg Burghardt Dec 07 '18 at 16:05

1 Answers1

0

Find the DIV by XPATH such that it contains the text you are looking for:

public WebElement savedText() {
    String xpath = "//div[contains(@class, 'statusMsg'][contains(., 'Your changes are saved.')]";

    return webdriver.findElement(By.xpath(xpath));

Selenium should implicitly wait for the DIV matching that class name, and containing the given text. The findElement method should return only when both conditions are true, which should combat race conditions in your test.

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92