我有一个带动态数字的元素。它是每个订单后生成的唯一订单号。我试图使用getText()方法从段落<p>
标记中获取该数字,但webdriver在测试运行后返回空白文本。
执行:
boolean a = driver.findElement(By.xpath("(.//*[@id='alert-message']/div/p/strong)[1]")).isDisplayed();
返回false,表示元素未显示。
这是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>
我尝试使用以下方式获取文字:
String orderNo = driver.findElement(By.xpath("(.//*[@id='alert-message']/div/p/strong)[1]")).getText();
System.out.println(orderNo);
我尝试了各种替代组合,而不是.getText()
,包括:
.getAttribute("textContent");
.getAttribute("innerHTML");
.getAttribute("value"); //retuns NULL value
.getAttribute("outerText");
但他们都没有奏效。如何在HTML中获取该订单号?
答案 0 :(得分:0)
获取p标签中的所有文本确实有效,我得到了一个 输出:&#34;生成了编号为JJ-MD000-12345的新订单。该 订单现已完成。&#34;我的意图是只获取订单号 这是一个空白的。有什么想法吗?
如果只有订单号是动态的,我建议如下:
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
答案 1 :(得分:0)
根据您共享的HTML,所需的元素是 Angular 元素,因此您可以将 WebDriverWait 与 ExpectedConditions 设置为{{3然后根据以下代码块提取/打印动态值:
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"));
答案 2 :(得分:0)
考虑使用显式等待并检查元素是否可见。弹出窗口打开后,您将尝试在角度有机会更新绑定之前访问该文本。结果你得到空文本。