[英]How can I extract the text from a variable with Selenium WebDriver?
If I use "Inspect Element" on FireFox, I see this: 如果在FireFox上使用“检查元素”,则会看到以下内容:
<span class="stock-number-value ng-binding">17109 </span>
When I use driver.getSource(), that 17109 is replaced by "vehicle.attributes.stockNumber". 当我使用driver.getSource()时,该17109被替换为“ vehicle.attributes.stockNumber”。
My main goal is to use the driver to get whatever value is stored by vehicle.attributes.stockNumber, but I can't figure out how to get the contents of that variable using Selenium. 我的主要目标是使用驱动程序来获取vehicle.attributes.stockNumber存储的任何值,但是我无法弄清楚如何使用Selenium获取该变量的内容。
I suspect you are getting the source too early in the process while the angular is not yet ready and the bindings are not yet data-feeded. 我怀疑您在角度尚未准备好并且绑定尚未进行数据馈送的过程中太早地获取源。 I'd use a custom wait condition function to wait for the "stock" to have a numeric value: 我将使用自定义的等待条件函数来等待“股票”具有数值:
WebDriverWait wait = new WebDriverWait(webDriver, 510);
WebElement stock = wait.until(waitForStock(By.cssSelector(".stock-number-value")));
System.out.println(stock.getText());
where waitForStock
is something along these lines: 其中waitForStock
类似于以下内容:
public static ExpectedCondition<Boolean> waitForStock(final By locator) {
return new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
try {
WebElement elm = driver.findElement(locator);
return elm.getText().trim().matches("[0-9]+");
} catch (NoSuchElementException e) {
return false;
} catch (StaleElementReferenceException e) {
return false;
}
}
@Override
public String toString() {
return "stock is not yet loaded";
}
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.