@lihuazhang 恒温,我今天也遇到一个问题,看看你有没有经验?
我需要获取在某些文字后面跟着的数字,例如余额后面的一个金额,我的想法是先试用文字定位到一个元素,然后找到它后面的以 0~9 开头的一个元素,再获取它的 Text,用的 Xpath 是:
public String getElementNumber(String text){
String xpath = "//*[starts-with(translate(@text,' ',''),'%s') or starts-with(translate(@content-desc,' ',''),'%s') or starts-with(translate(text(),' ',''),'%s')][%s]";
String xpath2 = xpath + "/following::*[starts-with(translate(text(),' ',''), '0') or starts-with(translate(text(),' ',''), '1') or starts-with(translate(text(),' ',''), '2') or starts-with(translate(text(),' ',''), '3') or starts-with(translate(text(),' ',''), '4') or starts-with(translate(text(),' ',''), '5') or starts-with(translate(text(),' ',''), '6') or starts-with(translate(text(),' ',''), '7') or starts-with(translate(text(),' ',''), '8') or starts-with(translate(text(),' ',''), '9')][1]";
System.out.println(String.format(xpath2, text, text,text,1));
return driver.findElementByXPath(String.format(xpath2, text, text,text,1)).getText();
}
问题是定位不到那个数字的元素,求教一下 T_T