我有以下方法:
}
protected String getCertID() {
String certIDKey = "certID";
TestAction action = new TestAction(ActionType.SET_ELEMENT_TEXT,springContext);
action.setIdentifierType("xpath");
action.setIdentifierValue("//h1[contains(@data-id,'header_title')]");
action.setValue(certIDKey);
action.perform(driverFactory,testContext);
log.info("CertID: " testContext.getVariable(certIDKey));
return testContext.getVariable(certIDKey);
}
此方法回傳此值:21/2/467092-已保存
我要求該方法僅回傳:21/2/467092 并忽略回傳的元素的 - Saved 部分
uj5u.com熱心網友回復:
如果"- Saved"那里不變,您可以將其替換為"":
return testContext.getVariable(certIDKey).replace("- Saved", "")
uj5u.com熱心網友回復:
如果h1包含子元素,例如某些span元素,以及您想要放置在某些子元素中的確切文本,例如:
<h1 data-id="header_title">
<span id="data">21/2/467092</span>
<span>- Saved</span>
</h1>
您可以使用
"//h1[contains(@data-id,'header_title')]/span[1]"
或者
"//h1[contains(@data-id,'header_title')]/span[@id='date']"
否則,對于以下情況:
<h1 data-id="header_title">21/2/467092- Saved</h1>
或者
<h1 data-id="header_title">
21/2/467092
<span>- Saved</span>
</h1>
您只能調整結果文本 h1
...
String roughText = testContext.getVariable(certIDKey);
return roughText.split("-")[0].trim();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/396741.html
上一篇:為什么陣列不列印?
