Home > Net >  Xpath is present on the page but data in that xpath loads a second later, due to which my getText();
Xpath is present on the page but data in that xpath loads a second later, due to which my getText();

Time:09-29

I can use thread.sleep(); but my boss doesn't want me to do that, if i use wait webDriverWait.until(ExpectedConditions.presenceOfElementLocated it is not working as well because the xpath is present, it is just the data(Text) that appears in sometime.

CodePudding user response:

For that kind of situation apply Explicit wait such that the attribute of the tag is updated with some value.

wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("xpath/tag[contains(@Unit,'Abc')]")));

You can also refer This Link

CodePudding user response:

  • If you know what text will be presented then you can use textToBePresentInElementLocated method. Let's see below example,

HTML#1:

  <input class="google"> Text </input>

Wait till text presents if you know the expected text

wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//input[@class='google']"), "Text"));
  • If that value (abc) resides in an attribute then you can use below approach.

HTML#2:

  <input class="google" value="Text" >

Wait till attribute have some value

WebElement element = driver.findElement(By.xpath("//input[@class='google']"));
wait.until(ExpectedConditions.attributeToBeNotEmpty(element, "value"));

How to read properties file?

https://stackoverflow.com/a/69236087/7731623

Dynamic xPath:

/** Code to read properties file **/

String inputData = readProperties.getProperty("Data").trim();
driver.findElement(By.xpath("//input[@class='" inputData "']"));
        wait.until(ExpectedConditions.attributeToBeNotEmpty(element, "value"));
  • Related