【问题标题】: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
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]"
它将返回第一个文本节点。