0

I have an element with a dynamic number. It's a unique order number that gets generated after every order. I am trying to fetch that number from a paragraph, <p> tag using the getText() method, but webdriver returns a blank text after the test is run.

Executing :

boolean a = driver.findElement(By.xpath("(.//*[@id='alert-message']/div/p/strong)[1]")).isDisplayed();

returns false, suggesting the element is not displayed.

This is the HTML:

<div class="ng-scope" ng-include="templates[step]">
<div class="ng-scope" ng-controller="wReviewCtrl">
<div class="panel-alert ng-hide" ng-show="success<0">
<div class="panel-alert" ng-show="success>0 && hasRole('BUYER')">
<div class="alert-wrap in">
<div id="alert-message" class="alert alert-success" role="alert">
<div class="media">
<strong>Done!</strong>
<p>
A new order with Number
<strong class="ng-binding">JJ-MD000-12345</strong>  //Order number
 was generated. The order is now complete.
</p>
</div>
</div>
</div>
</div>
<div class="panel-alert ng-hide" ng-show="success>0 && hasRole('SELLER')">
<div class="alert-wrap in">
<div id="alert-message" class="alert alert-success" role="alert">
<div class="media">
<strong>Done!</strong>
<p>

I tried getting text using:

String orderNo = driver.findElement(By.xpath("(.//*[@id='alert-message']/div/p/strong)[1]")).getText();
System.out.println(orderNo);

I have tried all sorts of alternative combinations instead of .getText() including:

.getAttribute("textContent");
.getAttribute("innerHTML");
.getAttribute("value");  //retuns NULL value
.getAttribute("outerText");

But none of them worked. How can I get that order number in the HTML?

4

3 回答 3

1

Consider using explicit waits and check for element to become visible. Once the popup opens, you are trying to access the text before angular has a chance to update the binding. As a result you are getting empty text.

于 2018-05-14T08:35:05.587 回答
0

Getting the whole of the texts in the p tag does work, and I got an output of: "A new order with Number JJ-MD000-12345 was generated. The order is now complete." My intention is to get the order number only and this the one that returns blank. Any ideas?

I would suggest the following if only the order number is dynamic:

String s = "A new order with Number JJ-MD000-12345 was generated. The order is now complete."; // is the string, which you are getting from 'p' tag
String[] parts = s.split(" "); // split this string to parts
String output = parts[5]; // choose the part with your order number
System.out.println(output); // JJ-MD000-12345
于 2018-05-14T07:47:54.130 回答
0

As per the HTML you have shared the desired element is an Angular element so you can induce WebDriverWait with ExpectedConditions set as attributeToBeNotEmpty() and then extract/print the dynamic value as per the following code block :

new WebDriverWait(driver, 20).until(ExpectedConditions.attributeToBeNotEmpty(driver.findElement(By.xpath("//div[@class='alert alert-success' and @id='alert-message']/div[@class='media']//p//strong[@class='ng-binding']")), "innerHTML"));
System.out.println(driver.findElement(By.xpath("//div[@class='alert alert-success' and @id='alert-message']/div[@class='media']//p//strong[@class='ng-binding']")).getAttribute("innerHTML"));
于 2018-05-14T08:16:52.980 回答