元素 returns 空白的 Webdriver getText()
Webdriver getText() of an element returns blank
我有一个带有动态编号的元素。它是在每次下单后生成的唯一订单号。我正在尝试使用 getText() 方法从段落 <p>
标签中获取该数字,但是 webdriver returns 测试后的空白文本是 运行.
正在执行:
boolean a = driver.findElement(By.xpath("(.//*[@id='alert-message']/div/p/strong)[1]")).isDisplayed();
returns 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");
但其中 none 有效。我怎样才能在 HTML 中获得该订单号?
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?
如果只有订单号是动态的,我会建议如下:
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
根据您共享的 HTML,所需元素是 Angular 元素,因此您可以引入 WebDriverWait ExpectedConditions 设置为 attributeToBeNotEmpty()
然后 extract/print 动态值 根据以下代码块:
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"));
考虑使用显式等待并检查元素是否可见。弹出窗口打开后,您将尝试在 angular 有机会更新绑定之前访问文本。结果你得到的是空文本。
我有一个带有动态编号的元素。它是在每次下单后生成的唯一订单号。我正在尝试使用 getText() 方法从段落 <p>
标签中获取该数字,但是 webdriver returns 测试后的空白文本是 运行.
正在执行:
boolean a = driver.findElement(By.xpath("(.//*[@id='alert-message']/div/p/strong)[1]")).isDisplayed();
returns 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");
但其中 none 有效。我怎样才能在 HTML 中获得该订单号?
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?
如果只有订单号是动态的,我会建议如下:
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
根据您共享的 HTML,所需元素是 Angular 元素,因此您可以引入 WebDriverWait ExpectedConditions 设置为 attributeToBeNotEmpty()
然后 extract/print 动态值 根据以下代码块:
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"));
考虑使用显式等待并检查元素是否可见。弹出窗口打开后,您将尝试在 angular 有机会更新绑定之前访问文本。结果你得到的是空文本。