-2

I'm trying to verify a few things on a page and have a grid with header columns. I want to grab the text from those are part of the verification. For some reason it's returning empty and I don't understand why considering I'm using contains text to find the element.

     String expectedName = "Employee Name";
     String actualName = driver.findElement(By.xpath("//div[contains(text(), 'Employee Name')]")).getText();
     Assert.assertEquals(expectedName, actualName);

The error I get is this: java.lang.AssertionError: expected [Employee Name] but found []

The HTML:

<div class="dx-datagrid-text-content dx-text-content-alignment-left dx-header-filter-indicator" role="presentation">Employee Name</div>
WeVie
  • 568
  • 2
  • 9
  • 25

1 Answers1

-1

You seem to be close. To extract the textContent of the <div> element ideally you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:

  • cssSelector:

    String expectedName = "Employee Name";
    String actualName = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.dx-datagrid-text-content.dx-text-content-alignment-left.dx-header-filter-indicator"))).getText();
    Assert.assertEquals(expectedName, actualName);
    
  • xpath:

    String expectedName = "Employee Name";
    String actualName = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='dx-datagrid-text-content dx-text-content-alignment-left dx-header-filter-indicator']"))).getText();
    Assert.assertEquals(expectedName, actualName);
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • This doesn't seem like a good solution as there's no need to wait for the element to be visible. If the element wasn't already visible, `String actualName = driver.findElement(By.xpath("//div[contains(text(), 'Employee Name')]")).getText();` would throw an error, would it not? – WeVie Mar 02 '20 at 13:19
  • Hmmm, you are finding an element by it's text _Employee Name_ and then you are using `getText()` to extract the same text _Employee Name_? Can you tell us more about your usecase? – undetected Selenium Mar 02 '20 at 13:30
  • What do you mean by "usecase?" This is a sanity check to make sure the screen loads as it should and data is populated. I thought I would first confirm a few of the column headings are visible then check for the expected data. Is that what you're looking for? I don't care how I find the element, contains text seemed the cleanest way. – WeVie Mar 02 '20 at 13:34