例如: 我有一个text = "Non Spécifié"
,我需要逃避口音匹配xpath
与locator
在page
如何用xpath selenium和java中的口音转义文本?
option = By.xpath("//div[./input[contains(@name, text)]]");
相关返回一个error
,因为不匹配xpath
。
显示为无Spécifié:enter image description here
可能是什么解决办法吗?
例如: 我有一个text = "Non Spécifié"
,我需要逃避口音匹配xpath
与locator
在page
如何用xpath selenium和java中的口音转义文本?
option = By.xpath("//div[./input[contains(@name, text)]]");
相关返回一个error
,因为不匹配xpath
。
显示为无Spécifié:enter image description here
可能是什么解决办法吗?
喜请尝试像下面将工作 使用java.text.Normalizer
// your accent string
String text = "Non Spécifié";
System.out.println("Earlier value : "+text);
// after using Normalizer
String string = Normalizer.normalize(text, Normalizer.Form.NFD);
// now compare each character against a letter.
String normalString = string.replaceAll("[^\\p{ASCII}]", "");
System.out.println("Accent changed to normal string : "+ normalString);
// note If your text is in unicode.use like below
// String normalString = string.replaceAll("\\p{M}", "");
// now you can use above in your xpath like below
option = By.xpath("//div[./input[contains(@name, normalString)]]");
希望这可以帮助你
回报失败: 早些时候值:非Spécifié ,口音改为正常的字符串:非SpAcifiA //div[./input[contains(@name, '非spacifia')]] 我尝试以下文本和工作在xpath中,但我不能使用文本硬编码: if(text.contains(“Non Sp”)){ text =“Non Sp \ u00E9cifi \ u00E9”; System.out.println(“text value”+ text); } –
检查了这一点:
//*[contains(translate(@name,'áàâäéèêëíìîïóòôöúùûü','aaaaeeeeiiiioooouuuu'),'Non Specifie')]
主要思路使用内置的xpath函数。理论上它应该够了。
祝你好运。
你可以附加HTML代码片段吗? Mayve你想包含(文本(),文本)而不是包含(@name,文本)? – DMart