【问题标题】:Unable to get the value in double quotes that is dynamically displayed using selenium xpath无法获取使用 selenium xpath 动态显示的双引号中的值
【发布时间】:2018-11-18 16:54:17
【问题描述】:

我正在尝试使用 selenium 中的 xpath 在下面的代码中动态显示值“19.5”。谁能帮我用 xpath 来获得 19.5 的值,我是 selenium 的新手。

<li>
  <label for="applyleave_leaveBalance">Leave Balance</label>
  <div id="applyleave_leaveBalance" class>
    "19.50"
    <a href="#balance_details" data-toggle="modal" id="leaveBalance_details_link">view details</a>
 </div>
</li>

【问题讨论】:

  • 哪种语言绑定? Java/Python/C#?

标签: java selenium xpath css-selectors webdriverwait


【解决方案1】:

更多的 outerHTML 会使解决方案更容易一些。但是,根据您提供的 HTML 提取值 19.5 您需要诱导 WebDriverWait 以使所需的 元素可见 并且您可以使用以下任一解决方案:

  • cssSelector:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("table[aria-label='User']")));
    String myText = ((JavascriptExecutor)driver).executeScript('return arguments[0].firstChild.textContent;', myElement).toString();
    
  • xpath:

    WebElement myElement = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//label[contains(.,'Leave Balance')]//following::div[@id='applyleave_leaveBalance']")));
    String myText = ((JavascriptExecutor)driver).executeScript('return arguments[0].firstChild.textContent;', myElement).toString();
    

【讨论】:

  • 谢谢,它成功了。我现在可以提取价值了。
【解决方案2】:

请按照以下步骤 (1) 以定位器为

@FindBy(xpath="//div[@id='applyleave_leaveBalance']")
private WebElement balance;

public void printBalance()
{
System.out.println(balance.getText());
}

【讨论】:

  • 这也会提取子链接的文本
  • 非常感谢。
【解决方案3】:

尝试使用这个

xpath="//div[@id='applyleave_leaveBalance']/text()[1]"

它将返回第一个文本节点。

【讨论】:

  • 谢谢,我知道了。
猜你喜欢
  • 1970-01-01
  • 2016-07-28
  • 1970-01-01
  • 1970-01-01
  • 2021-06-19
  • 1970-01-01
  • 1970-01-01
  • 2020-08-25
  • 1970-01-01
相关资源
最近更新 更多