简体   繁体   English

如何使用Selenium WebDriver从变量中提取文本?

[英]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.

相关问题 如何使用Selenium Webdriver和Java从元素中获取文本? - How can I get text from element with Selenium webdriver and Java? 如何使用Selenium Webdriver识别特定文本上的语言 - How can I identify the language on a specific text using Selenium Webdriver 如何在 Selenium WebDriver 中获取元素的文本? - How can I get text of an element in Selenium WebDriver? 如何使用selenium webdriver从一个文档中选择文本 - How can i select text from one document using selenium webdriver 如何使用Selenium Webdriver从“ span”标签获取文本? - How can I get text from a “span” tag using Selenium Webdriver? 如果我通过Selenium Webdriver和Java知道相应的文本,如何提取DOM元素的ID属性 - How to extract the ID attribute of a DOM element, if i know corresponding text through Selenium Webdriver and Java 如何通过 Java 使用 Selenium Webdriver 从下拉列表中提取 firstselectedoption 的文本 - How to extract the text of the firstselectedoption from a dropdown using Selenium Webdriver through Java 如何在Java中从Selenium Webdriver调用小书签? - How can I call a bookmarklet from Selenium webdriver in java? 如何使用Selenium Webdriver中的操作类提取文本? - How to extract text using action class in selenium webdriver? 如何<nobr>使用 selenium webdriver</nobr>提取里面的动态文本<nobr>?</nobr> - How to extract the dynamic text inside <nobr> using selenium webdriver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM